8

if i expand any item in the recyclerview it expands fine but when i scroll down the recylerview i found other items also expanded , due to recycling it took the expanded size not the original one

 public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.MyViewHolder> implements View.OnClickListener {

    private LayoutInflater inflater;
    private Context mcontext;
    private int mOriginalHeight = 0;
    private boolean mIsViewExpanded = false;

    public FeedAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        mcontext = context;

    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View view = inflater.inflate(R.layout.custom_row, viewGroup, false);
        MyViewHolder holder = new MyViewHolder(view);
        holder.frame.setOnClickListener(this);


        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder myViewHolder, int i) {


    }

    @Override
    public int getItemCount() {
        return 100;
    }

    @Override
    public void onClick(final View v) {
        if (mOriginalHeight == 0) {
            mOriginalHeight = v.getHeight();
        }
        ValueAnimator valueAnimator;
        if (v.getHeight() < (mOriginalHeight + (int) (mOriginalHeight * 1.5))) {
            valueAnimator = ValueAnimator.ofInt(mOriginalHeight, mOriginalHeight + (int) (mOriginalHeight * 1.5));
        } else {
            valueAnimator = ValueAnimator.ofInt(mOriginalHeight + (int) (mOriginalHeight * 1.5), mOriginalHeight);
        }
        valueAnimator.setDuration(300);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                Integer value = (Integer) animation.getAnimatedValue();
                v.getLayoutParams().height = value.intValue();
                v.requestLayout();
            }
        });
        valueAnimator.start();

    }

    class MyViewHolder extends RecyclerView.ViewHolder {


        FrameLayout frame;

        public MyViewHolder(View itemView) {

            super(itemView);
            frame = (FrameLayout) itemView.findViewById(R.id.base);

        }
    }
}

so how do i fix that and also is there a better way to expand and collapse items ?

Prudhvi
  • 2,276
  • 7
  • 34
  • 54
mohab elmahdy
  • 173
  • 1
  • 1
  • 10
  • Can you elaborate how you achieved the effect ? – borax12 Apr 23 '15 at 10:13
  • the code i use was not efficient as it expands the cell to a specific size .a better way to expand the cells is to use the code [Here](http://stackoverflow.com/a/13381228/4561402/) as it expands it to wrap content , because cells are not of the same height – mohab elmahdy Apr 25 '15 at 13:58
  • @mohabelmahdy can you provide the entire working solution!!PLease – williamj949 Jun 04 '15 at 18:42

1 Answers1

11

I ran into the same issue. This happens because of the ViewHolderPattern.

https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html#isRecyclable%28%29

Android will recycle the view, regardlessly if it's expanded or not. So tell android that if your view is expanded, it is not recyclable.

In Your ViewHolder Class add:

public MyViewHolder(View itemView) {

   super(itemView);
   frame = (FrameLayout) itemView.findViewById(R.id.base);

   frame.setTag(this); // to get a reference to viewholder later on
}

In Your OnClick Method add:

@Override

public void onClick(final View v) {

   MyViewHolder holder = (MyViewHolder) v.getTag();
   holder.setIsRecyclable(false);


    if (mOriginalHeight == 0) {
        mOriginalHeight = v.getHeight();
    }
    ValueAnimator valueAnimator;
    ...
}

This should solve Your Problem. Remember to set "setIsRecyclable()" to true again after the view isn't expanded any more so that android can recycle it. Hope I could help.

Cheers

Florestan

oriolparra
  • 212
  • 6
  • 21
Florestan
  • 126
  • 1
  • 3