android – 扩展recyclerview项目

前端之家收集整理的这篇文章主要介绍了android – 扩展recyclerview项目前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我展开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类中添加

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可以回收它.
希望我能提供帮助.

干杯

弗洛雷斯坦

原文链接:https://www.f2er.com/android/309016.html

猜你在找的Android相关文章