从RecyclerView中删除项目可能会导致视图重叠,如此视频所示 链接
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();
时,似乎解决了我的问题,但是移除动画被破坏了。
我试图找到解决方案来解决这个问题,但似乎没有人与我有同样的问题。谢谢回答。@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);
}
}