Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Expandable RecyclerView with Animation in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 1.19k
    Comment on it

    Expandable RecyclerView contains two views one is parent view and other is child view. Parent is visible by default but the child view has to be expanded and collapsed. It will expand when we click on parent view.

    Let's see the Implementation of Expandable RecyclerView with Animation.


     

     

     To Use RecyclerView in your project compile recyclerview library in your projects Gradle (app level).

     

        implementation 'com.android.support:recyclerview-v7:26.1.0'

     Take ListView height "0" in xml and calculate its height of the basis of subitems in data and set listview on RecyclerView's item Click.

    holder.itemView.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
     listAdapter = new ListAdapter(context,data);
    
     if(holder.listView.getHeight()==0){
     holder.listView.setAdapter(listAdapter);
     setListViewHeightBasedOnItems(holder.listView,true);
     }else{
     setListViewHeightBasedOnItems(holder.listView,false);
     }
     }
     });
     /**
     * method to calculate the height of listview
     * @param listView
     * @param visible
     * @return
     */
     public boolean setListViewHeightBasedOnItems(final ListView listView,boolean visible) {
    
     if (listAdapter != null) {
    
     int numberOfItems = listAdapter.getCount();
    
     // Get total height of all items.
     int totalItemsHeight = 0;
     for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
     View item = listAdapter.getView(itemPos, null, listView);
     float px = 500 * (listView.getResources().getDisplayMetrics().density);
     item.measure(View.MeasureSpec.makeMeasureSpec((int)px, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
     totalItemsHeight += item.getMeasuredHeight();
     }
    
     // Get total height of all item dividers.
     int totalDividersHeight = listView.getDividerHeight() *
     (numberOfItems - 1);
     // Get padding
     int totalPadding = listView.getPaddingTop() + listView.getPaddingBottom();
    
     listViewVisbility(visible,listView,totalItemsHeight,totalDividersHeight,totalPadding);
     return true;
    
     } else {
     return false;
     }
    
     }
    
    
    
     /**
     * method to set animation
     * @param visible
     * @param listView
     * @param totalItemsHeight
     * @param totalDividersHeight
     * @param totalPadding
     */
     public void listViewVisbility(boolean visible,final ListView listView,int totalItemsHeight,int totalDividersHeight,int totalPadding){
    
     final ViewGroup.LayoutParams params = listView.getLayoutParams();
     params.height = totalItemsHeight + totalDividersHeight + totalPadding;
     listView.setLayoutParams(params);
     listView.requestLayout();
    
     ValueAnimator widthAnimator = ValueAnimator.ofInt(0, params.height);
     if (visible)
     widthAnimator = ValueAnimator.ofInt(0, params.height);
     else
     widthAnimator = ValueAnimator.ofInt( params.height,0);
     widthAnimator.setDuration(300);
     widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
     @Override
     public void onAnimationUpdate(ValueAnimator animation) {
     int animatedValue = (int) animation.getAnimatedValue();
     listView.getLayoutParams().height = animatedValue;
     listView.requestLayout();
     }
     });
     widthAnimator.start();
     }
    
    


    For SourceCode kindly find attachment

    Expandable RecyclerView with Animation in Android

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: