问题
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);
}
}
回答1:
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.
回答2:
On Your Activity level, where you are binding your adapter with the Recycler View, You need to clear your list before calling notifyDataSetChanged().
Use the following line of code
adapter.clearData();
and
adapter.notifyDataSetChanged();
And in deleteItem() method:-
void deleteItem(int index) {
postList.remove(index);
notifyItemRemoved(index);
notifyItemRangeChanged(index, getItemCount() - index);
}
For more help you can follow this link:- https://github.com/kanytu/android-parallax-recyclerview/issues/13
回答3:
Try this
void deleteItem(int index) {
postList.remove(index);
notifyItemRemoved(index);
//notify items from index to end
notifyItemRangeChanged(index, getItemCount() - index);
}
来源:https://stackoverflow.com/questions/46313911/overlap-view-after-delete-item-in-recyclerview