Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlap view after delete item in RecyclerView

Removing item in RecyclerView cause view overlap like this video Link

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" />

In Adapter

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);

}

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

EDIT

@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);
    }
}
like image 308
Khdkls Avatar asked Sep 20 '17 05:09

Khdkls


1 Answers

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);

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

like image 80
Sharath kumar Avatar answered Oct 18 '22 05:10

Sharath kumar