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:

RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
MyAdapter adapter = new MyAdapter(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:

static class GroupViewHolder extends ExpandableRecyclerView.GroupViewHolder {
        ImageView expandedIndicator;
        TextView text1;
        TextView text2;
        private boolean expanded;


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

            itemView.setLayoutParams(new RecyclerView.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);
        }

        public void expand() {
            ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
            animator.setInterpolator(new DecelerateInterpolator());
            animator.setDuration(200);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ViewHelper.setRotation(expandedIndicator, 180 * (float) (animation.getAnimatedValue()));
                    expandedIndicator.postInvalidate();
                }
            });
            animator.start();
            expanded = true;
        }

        public void collapse() {
            ValueAnimator animator = ValueAnimator.ofFloat(1, 0);
            animator.setInterpolator(new DecelerateInterpolator());
            animator.setDuration(200);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ViewHelper.setRotation(expandedIndicator, 180 * (float) (animation.getAnimatedValue()));
                    expandedIndicator.postInvalidate();
                }
            });
            animator.start();
            expanded = false;
        }

        @Override
        public boolean isExpanded() {
            return expanded;
        }

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

        void setText1(String t) {
            text1.setText(t);
        }


        void setText2(String t) {
            text2.setText(t);
        }
    }

Post a Comment for "Expandable RecycleView Header And Adapter"