如果我展开recyclerview中的任何项目它扩展正常,但当我向下滚动recylerview我发现其他项目也扩大,由于回收它采取扩大的尺寸而不是原来的
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); } } }
解决方法
我遇到了同样的问题.这是因为ViewHolderPattern.
Android将回收视图,无论它是否已扩展.
所以告诉android,如果你的视图被扩展,它是不可回收的.
在您的ViewHolder类中添加:
public MyViewHolder(View itemView) { super(itemView); frame = (FrameLayout) itemView.findViewById(R.id.base); frame.setTag(this); // to get a reference to viewholder later on }
@Override public void onClick(final View v) { MyViewHolder holder = (MyViewHolder) v.getTag(); holder.setIsRecyclable(false); if (mOriginalHeight == 0) { mOriginalHeight = v.getHeight(); } ValueAnimator valueAnimator; ... }
这应该解决你的问题.
记得在视图不再展开后再次将“setIsRecyclable()”设置为true,以便android可以回收它.
希望我能提供帮助.
干杯
弗洛雷斯坦