扩展recyclerview项目

时间:2015-03-15 00:36:59

标签: android android-animation 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);

        }
    }
}

所以 我如何解决这个问题,还有更好的方法来扩展和折叠项目吗?

2 个答案:

答案 0 :(得分:10)

我遇到了同样的问题。这是因为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可以回收它。 希望我能提供帮助。

干杯

弗洛雷斯坦

答案 1 :(得分:0)

您只需更改代码即可获得最大的可读性。因此,您必须使用(?<!\?)\?{1}(?!.*?\?)|(?<=\?)\?{1}(?!.*?\?)方法而不是值动画。请检查my answer。这样做可以节省大量时间。