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 ?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With