Recyclerview element extension

if I expand any element in recyclerview, it expands fine, but when I look at recylerview, I find that other elements have also been expanded, due to reuse it took an extended size, not the original

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 can i fix this and is there also a better way to expand and collapse the elements?

+6
source share
2 answers

I ran into the same problem. This is due to ViewHolderPattern.

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

Android will redesign the view, whether it has expanded or not. Therefore, tell Android that if your presentation 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 no longer expands so that android can recycle it. Hope I can help.

Greetings

Florestan

+8
source

You can simply change your code to get maximum readability. Therefore, you should use the notifyItemChanged() method instead of the value animator. Mark my answer . You can save a lot of time by doing this.

0
source

Source: https://habr.com/ru/post/983806/


All Articles