删除RecyclerView中的项目导致视图重叠,就像这个视频一样 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" />
在适配器
中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);
}
}
答案 0 :(得分:3)
在onBindViewHolder中添加clickListener并更改代码,如下所示
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);
删除时始终从datalist中删除项目,然后再通知adsAdapter。 当每个查看者数据具有不同的数据状态时,也使用holder.setIsRecyclable(false)。
答案 1 :(得分:0)
试试这个
void deleteItem(int index) {
postList.remove(index);
notifyItemRemoved(index);
//notify items from index to end
notifyItemRangeChanged(index, getItemCount() - index);
}
答案 2 :(得分:0)
在您的活动级别上,您将适配器与Recycler View绑定在一起,您需要在调用notifyDataSetChanged()之前清除列表。
使用以下代码行
adapter.clearData();
和
adapter.notifyDataSetChanged();
在deleteItem()方法中: -
void deleteItem(int index) {
postList.remove(index);
notifyItemRemoved(index);
notifyItemRangeChanged(index, getItemCount() - index);
}
如需更多帮助,请点击以下链接: - https://github.com/kanytu/android-parallax-recyclerview/issues/13