扩展和折叠RecyclerView项目显示错误行为

时间:2017-03-12 18:53:00

标签: android

我正在构建一个Android项目,我使用RecyclerView来显示列表项。列表项是CardViews。我在每张卡片上都有两个可点击的图标,一个负责展开卡片并显示剩余的视图,另一个负责将卡片折叠到之前的状态。

我的问题是,当我展开一张卡片(例如:第一张卡片)并向下滚动时,我看到另一张卡片(例如:第10张卡片)已经展开。我知道,这是因为第一张牌被回收并用来代替第10张牌。但由于我没有点击第10张卡片中的展开图标,因此它应处于折叠状态。我怎样才能做到这一点?

如果这个问题非常幼稚,我很抱歉。

这是我的RecyclerViewAdapter代码。

private List<String> mDataSet;

public class ViewHolder extends RecyclerView.ViewHolder {

    public CardView cardView;
    public TextView headLine, newsPaperName, time, newsDetail;
    public ImageView newsImage, iconExpand, iconCollapse;
    public int minHeight, maxHeight;

    public ViewHolder(final View itemView) {
        super(itemView);

        cardView = (CardView) itemView.findViewById(R.id.new_item_card_view);
        newsImage = (ImageView) itemView.findViewById(R.id.news_image);
        headLine = (TextView) itemView.findViewById(R.id.headline);
        newsPaperName = (TextView) itemView.findViewById(R.id.news_paper);
        time = (TextView) itemView.findViewById(R.id.time);
        iconExpand = (ImageView) itemView.findViewById(R.id.icon_expand);
        iconCollapse = (ImageView) itemView.findViewById(R.id.icon_collapse);
        newsDetail = (TextView) itemView.findViewById(R.id.news_detail);


        cardView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                cardView.getViewTreeObserver().removeOnPreDrawListener(this);
                // Set the minimum height of the CardView
                minHeight = cardView.getHeight() - newsDetail.getMeasuredHeight();
                // Set the maximum height of the card view
                maxHeight = cardView.getHeight();

                ViewGroup.LayoutParams layoutParams = cardView.getLayoutParams();
                layoutParams.height = minHeight;
                cardView.setLayoutParams(layoutParams);

                return true;
            }
        });

        iconExpand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expandView();
            }
        });

        iconCollapse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                collapseView();
            }
        });
    }

    private void collapseView() {

        ValueAnimator anim = ValueAnimator.ofInt(cardView.getMeasuredHeightAndState(), minHeight);
        anim.setDuration(150);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = cardView.getLayoutParams();
                layoutParams.height = val;
                cardView.setLayoutParams(layoutParams);
                iconExpand.setVisibility(View.VISIBLE);
                iconCollapse.setVisibility(View.INVISIBLE);
            }
        });
        anim.start();
    }
    private void expandView() {
        newsDetail = (TextView) itemView.findViewById(R.id.news_detail);

        ValueAnimator anim = ValueAnimator.ofInt(cardView.getMeasuredHeightAndState(), maxHeight);
        anim.setDuration(150);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = cardView.getLayoutParams();
                layoutParams.height = val;
                cardView.setLayoutParams(layoutParams);
                iconCollapse.setVisibility(View.VISIBLE);
                iconExpand.setVisibility(View.INVISIBLE);

            }
        });
        anim.start();
    }
}

public MyRecyclerViewAdapter(List<String> myDataSet) {
    mDataSet = myDataSet;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    holder.headLine.setText("Torem ipsum dolor sit amet, consectetur adipiscing eli");
    holder.newsPaperName.setText("Crux: Covering all things Catholic");
    holder.time.setText(" - 54 min. ago");
    holder.newsDetail.setText("NEW York The U.S. Securities and exchange Commission on Friday" +
            "denied a request to list what would");
}

@Override
public int getItemCount() {
    return mDataSet.size();
}

}

1 个答案:

答案 0 :(得分:0)

`

@Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

这里返回position而不是super.getItemId()

并实现setHasStableIds(true);`