扩大recyclerview项目

expanding recyclerview item

如果我在 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。

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

Android 将回收视图,无论它是否展开。 所以告诉android,如果你的视图被展开,它是不可回收的。

在您的 ViewHolder Class 添加:

public MyViewHolder(View itemView) {

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

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

在您的 OnClick 方法中添加:

@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 可以回收它。 希望能帮到你。

干杯

弗洛雷斯坦