Skip to content Skip to sidebar Skip to footer

Expandable Recycleview Header And Adapter

I am using this library to make a expandable RecyclerView I made my own version of it so I can define the header but It's giving me I think I made something wrong because It's givi

Solution 1:

I got an answer for these questions:

  • How to set my one item or more to be expanded as I open the activity.

before setting the adapter for the RecyclerView I used expand() method as the following:

RecyclerViewrv= (RecyclerView) findViewById(R.id.rv);
MyAdapteradapter=newMyAdapter(getContext(), R.layout.group_header);
adapter.expand(0); // 0 will expand the first Item.
rv.setAdapter(adapter);

  • a way to make my own header layout.

all you need to do is create your own group header as the following:

staticclassGroupViewHolderextendsExpandableRecyclerView.GroupViewHolder {
        ImageView expandedIndicator;
        TextView text1;
        TextView text2;
        privateboolean expanded;


        GroupViewHolder(Context context, int layout) {
            super(View.inflate(context, layout, null));

            itemView.setLayoutParams(newRecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            expandedIndicator = (ImageView) itemView.findViewById(R.id.groupExpandedIndicator);
            text1 = (TextView) itemView.findViewById(R.id.text1);
            text2 = (TextView) itemView.findViewById(R.id.text2);
        }

        publicvoidexpand() {
            ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
            animator.setInterpolator(newDecelerateInterpolator());
            animator.setDuration(200);
            animator.addUpdateListener(newValueAnimator.AnimatorUpdateListener() {
                @OverridepublicvoidonAnimationUpdate(ValueAnimator animation) {
                    ViewHelper.setRotation(expandedIndicator, 180 * (float) (animation.getAnimatedValue()));
                    expandedIndicator.postInvalidate();
                }
            });
            animator.start();
            expanded = true;
        }

        publicvoidcollapse() {
            ValueAnimator animator = ValueAnimator.ofFloat(1, 0);
            animator.setInterpolator(newDecelerateInterpolator());
            animator.setDuration(200);
            animator.addUpdateListener(newValueAnimator.AnimatorUpdateListener() {
                @OverridepublicvoidonAnimationUpdate(ValueAnimator animation) {
                    ViewHelper.setRotation(expandedIndicator, 180 * (float) (animation.getAnimatedValue()));
                    expandedIndicator.postInvalidate();
                }
            });
            animator.start();
            expanded = false;
        }

        @OverridepublicbooleanisExpanded() {
            return expanded;
        }

        publicvoidsetExpanded(boolean expanded) {
            ViewHelper.setRotation(expandedIndicator, expanded ? 180 : 0);
            this.expanded = expanded;
        }

        voidsetText1(String t) {
            text1.setText(t);
        }


        voidsetText2(String t) {
            text2.setText(t);
        }
    }

Post a Comment for "Expandable Recycleview Header And Adapter"