简体   繁体   English

删除项目后,RecyclerView不会更新

[英]RecyclerView does not update after removing an item

I have a RecyclerView horizontal image slider at the bottom of a fragment. 我在片段底部有一个RecyclerView水平图像滑块。 The top of the fragment shows some details. 片段的顶部显示了一些细节。 Once the user clicks on the images at the bottom, the idea is to remove that image from the image slider and display its information in the fragment. 一旦用户点击底部的图像,想法是从图像滑块中删除该图像并在片段中显示其信息。 Now the information shows up but the image does not gets removed from the RecyclerView . 现在信息显示但图像不会从RecyclerView删除。 Here is what I have coded in the Onclick of the outermost layout. 这是我在最外层布局的Onclick中编码的内容。 I have tried all the related answers that I could find but nothing worked. 我已经尝试了所有可以找到的相关答案,但没有任何效果。 They all are in the code. 它们都在代码中。 Please let me know what am I doing wrong or what is missing. 请让我知道我做错了什么或错过了什么。

holder.itemRowRelativeLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (isFiltering) {
                mItemList.clear();
                mItemList.addAll(mOriginalItemList);
                mItemList.remove(position);// At this point mItemList holds the correct. That is all the images but not the one that was clicked. 

                notifyItemRemoved(position); //solution 1
                notifyItemRangeRemoved(position, getItemCount()); // solution 2
                notifyItemRangeRemoved(0, getItemCount()); // solution 3
                notifyDataSetChanged();//solution 4
            }
        }
    });

Full Code of the adapter 适配器的完整代码

public class ImageGallery16X9Adapter<T extends GalleryItem> extends RecyclerView.Adapter<ImageGallery16X9Adapter.GalleryItemViewHolder> {

public enum GalleryMode {
    All_SAME,
    FIRST_DIFFERENT
}

private Context mContext;
private BasePresenter mPresenter;
private List<T> mItemList;
private List<T> mOriginalItemList;
private GalleryItem mFirstItem;
private GalleryMode mGalleryMode;
private int deviceWidth, itemWidth, marginSingle, marginDouble;
private boolean isFiltering;

public ImageGallery16X9Adapter(Context context, BasePresenter presenter, GalleryMode galleryMode, List<T> itemList, GalleryItem firstItem, boolean isFiltering) {
    mContext = context;
    mPresenter = presenter;
    mGalleryMode = galleryMode;
    mItemList = new ArrayList<>(itemList);
    mOriginalItemList = new ArrayList<>(itemList);
    mFirstItem = firstItem;
    deviceWidth = CommonUtils.getDeviceWidth((Activity) mContext);
    itemWidth = (int) (deviceWidth * 0.9);
    marginDouble = (int) (deviceWidth * 0.05);
    marginSingle = (int) (deviceWidth * 0.025);
    this.isFiltering = isFiltering;
}

@Override
public GalleryItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new GalleryItemViewHolder(LayoutInflater.from(parent.getContext()).
            inflate(R.layout.row_image_gallery_16x9_item, parent, false));
}

@Override
public void onBindViewHolder(GalleryItemViewHolder holder, final int position) {
    RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) holder.itemRowRelativeLayout.getLayoutParams();
    RelativeLayout.LayoutParams rlParams = (RelativeLayout.LayoutParams) holder.itemImageView.getLayoutParams();
    layoutParams.width = itemWidth;
    rlParams.height = (int) (layoutParams.width * Constant.HEIGHT_FACTOR_16X9);

    if (position == 0) {
        layoutParams.leftMargin = marginDouble;
        layoutParams.rightMargin = 0;
        if (mGalleryMode == GalleryMode.FIRST_DIFFERENT) {
            holder.itemTitle.setVisibility(View.VISIBLE);
            holder.itemTitle.setText(mFirstItem.getItemTitle());
            if (mFirstItem.getItemImage() != null) {
                Picasso.with(MyApplication.getAppContext()).load(mFirstItem.getItemImage()).fit().placeholder(R.drawable.error_image).error(R.drawable.error_image).into(holder.itemImageView);
            } else {
                Picasso.with(MyApplication.getAppContext()).load(R.drawable.error_image).placeholder(R.drawable.error_image).error(R.drawable.error_image).fit().into(holder.itemImageView);
            }
            holder.itemDescription.setText(mFirstItem.getItemDescription());
        }
    } else {
        if (mGalleryMode == GalleryMode.FIRST_DIFFERENT) {
            if (position == mItemList.size()) {
                layoutParams.rightMargin = marginDouble;
            } else {
                layoutParams.rightMargin = 0;
            }
        } else {
            if (position == mItemList.size() - 1) {
                layoutParams.rightMargin = marginDouble;
            } else {
                layoutParams.rightMargin = 0;
            }
        }
        layoutParams.leftMargin = marginSingle;
    }

    int itemPosition = position;
    if (mGalleryMode == GalleryMode.FIRST_DIFFERENT && position > 0) {
        itemPosition = position - 1;
        T item = mItemList.get(itemPosition);
        holder.itemTitle.setVisibility(View.GONE);
        holder.itemDescription.setText(item.getItemDescription());
        Picasso.with(mContext).load(item.getItemImage()).fit().placeholder(R.drawable.error_image).error(R.drawable.error_image).into(holder.itemImageView);

    } else if (mGalleryMode == GalleryMode.All_SAME) {
        T item = mItemList.get(itemPosition);
        holder.itemTitle.setVisibility(View.GONE);
        holder.itemDescription.setText(item.getItemDescription());
        Picasso.with(mContext).load(item.getItemImage()).fit().placeholder(R.drawable.error_image).error(R.drawable.error_image).into(holder.itemImageView);
    }

    holder.itemRowRelativeLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mGalleryMode == GalleryMode.FIRST_DIFFERENT) {
                if (position == 0) {
                    mPresenter.onItemClicked(mFirstItem);
                } else {
                    mPresenter.onItemClicked(mItemList.get(position - 1));
                }
            } else {
                mPresenter.onItemClicked(mItemList.get(position));
                if (isFiltering) {
                    mItemList.clear();
                    mItemList.addAll(mOriginalItemList);
                    mItemList.remove(position);

                    notifyItemRemoved(position); //solution 1
                    notifyItemRangeRemoved(position, getItemCount()); // solution 2
                    notifyItemRangeRemoved(0, getItemCount()); // solution 3
                    notifyDataSetChanged();//solution 4
                }
            }
        }
    });
}

@Override
public int getItemCount() {
    if (mGalleryMode == GalleryMode.FIRST_DIFFERENT)
        return mItemList.size() + 1;
    else
        return mItemList.size();
}


static class GalleryItemViewHolder extends RecyclerView.ViewHolder {
    private final TextView itemDescription, itemTitle;
    private final ImageView itemImageView, itemFavoriteImageView;
    private final RelativeLayout itemRowRelativeLayout;

    public GalleryItemViewHolder(View itemView) {
        super(itemView);
        itemRowRelativeLayout = (RelativeLayout) itemView.findViewById(R.id.rl_gallery_item_row);
        itemImageView = (ImageView) itemView.findViewById(R.id.img_gallery_item);
        itemFavoriteImageView = (ImageView) itemView.findViewById(R.id.img_gallery_item_favorite);
        itemTitle = (TextView) itemView.findViewById(R.id.txt_gallery_item_name);
        itemDescription = (TextView) itemView.findViewById(R.id.txt_gallery_item_description);
    }
}

} }

You need to use this 3 lines to make it work 您需要使用这3行来使其工作

mItemList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mItemList.size());
private void removerecyclerItem(DraftStoriesPojo list) {
        int current_position = allStoriesPojoList.indexOf(list);
        allStoriesPojoList.remove(current_position);
        notifyItemRemoved(current_position);
        notifyItemRangeChanged (current_position, getItemCount());
    }

In order to have your code working you need to change Adapter constructor implementation as follows: 为了使代码正常工作,您需要更改Adapter构造函数实现,如下所示:

    public RecyclerViewAdapter(Context context, List<Model> model) {
    this.context = context;
    this.model = model;
}

Then in onActivityResult do like this: 然后在onActivityResult中这样做:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 4) {
        listModel.clear();
        listModel.addAll(repository.consDataBase(context));
        recyclerViewAdapter.notifyDataSetChanged();
    }
}

No need to do so much complicated things there,simply remove and notify 无需在那里做那么复杂的事情,只需删除并通知

holder.itemRowRelativeLayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (isFiltering) {
            mItemList.remove(position);
            notifyItemRemoved(position);
        }
    }
});

Only add these two lines 只添加这两行

mItemList.remove(position);
notifyDataSetChanged();

You need to use these lines to make it work 您需要使用这些线来使其工作

mItemList.remove(position);
notifyDataSetChanged();

Declare a method in your custom RecylerView like below 在您的自定义RecylerView声明一个方法,如下所示

public void DeleteData(int position){

        recordingItems.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, recordingItems.size());
    }

and from mainActivity call 并从mainActivity调用

adapter.DeleteData(position);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM