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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With