RecyclerView中删除项目后的重叠视图

RecyclerView中删除项目后的重叠视图

本文介绍了RecyclerView中删除项目后的重叠视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在RecyclerView中删除项目会导致视图重叠,例如此视频链接

Removing item in RecyclerView cause view overlap like this videoLink

fragment_feed.xml

fragment_feed.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none" />

在适配器中

holder.setIsRecyclable(false);
  ((PostViewHolder) holder).mUsername.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteItem(holder.getAdapterPosition());
            }
        });

void deleteItem(int index) {
    postList.remove(index);
    notifyItemRemoved(index);

}

当我将notifyItemRemoved(index);更改为notifyDataSetChanged();时,似乎可以解决我的问题,但这是导致删除动画被破坏的原因.我试图找到解决方案来解决此问题,但似乎没有人遇到同样的问题.感谢您的回答

When i change notifyItemRemoved(index); to notifyDataSetChanged(); seem to solved my problem but it's cause remove animation destroyed.I try to find solution to solve this but It's seem like no one have the same problem with me. Thanks for answer

编辑

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
    holder.setIsRecyclable(false);
    if (holder instanceof PostViewHolder) {
        Post post = (Post) postList.get(position);
        String type = post.getTypePost();

        // Inflate Layout //
        LayoutInflater inflater = LayoutInflater.from(mContext);


        ((PostViewHolder) holder).mUsername.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteItem(holder.getAdapterPosition());
            }
        });

        ((PostViewHolder) holder).mUsername.setText(post.getOwnerPost());


    } else if (holder instanceof HeaderViewHolder) {

    } else{
        ((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
    }
}

推荐答案

在onBindViewHolder中添加clickListener并按如下所示更改代码

Add the clickListener in onBindViewHolder and change the code like following

PostViewHolder postViewHolder=(PostViewHolder) holder;
postViewHolder.mUsername.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            deleteItem(holder.getAdapterPosition());
        }
    });

void deleteItem(int index) {
    postList.remove(index);
    notifyItemRemoved(index);
    notifyItemRangeChanged(position, yourDataSet.size());
}
holder.setIsRecyclable(true);

在删除时,总是从数据列表中删除项目,然后通知适配器.当每个观看者数据具有不同的数据状态时,也请使用holder.setIsRecyclable(false).

While deleting always remove item from datalist and then notifyAdapter.Also use holder.setIsRecyclable(false) when each viewholder data has different data state.

这篇关于RecyclerView中删除项目后的重叠视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:46