You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RecyclerView调用notifyItemInserted/Removed后出现随机显示异常求助

Fixing RecyclerView Display Glitches After Deleting/Adding Items

Hey there, I’ve gone through your code and spotted a few key issues that are causing those random display glitches in your RecyclerView. Let’s break them down and fix them step by step:

1. You’re Misusing getItemViewType

Overriding getItemViewType to return the item’s position is a big no-no here. This tells RecyclerView that every single item is a unique view type, completely breaking its ViewHolder reuse mechanism. When you delete or add items, the positions shift, and RecyclerView ends up reusing the wrong views with old data—leading to the messed-up display and deleted items reappearing.

Fix: Remove that getItemViewType override entirely. You only need this method if you actually have distinct view types (like headers and regular items), not just for every individual position.

2. Missing onBindViewHolder (Critical!)

Your Adapter code doesn’t include an onBindViewHolder method—this is how RecyclerView binds your latest data to the ViewHolder views. Without it, RecyclerView just reuses old ViewHolders with their existing text, which is exactly why deleted items pop back up when you add new ones.

Add these missing methods to your Adapter:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // Inflate your item layout (replace with your actual layout ID)
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.off_limits_item_layout, parent, false);
    return new OffLimitsViewHolder(itemView);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    OffLimitsViewHolder viewHolder = (OffLimitsViewHolder) holder;
    // Bind the current item's text to the TextView
    viewHolder.offLimitsTextViewItem.setText(offLimitsItemsList.get(position));
}

@Override
public int getItemCount() {
    // Return the size of your data list so RecyclerView knows how many items to render
    return offLimitsItemsList.size();
}

Make sure to replace R.layout.off_limits_item_layout with the actual layout ID you’re using for your RecyclerView items.

3. Minor Upgrade: Use getAbsoluteAdapterPosition()

The getAdapterPosition() method is deprecated. In your ViewHolder’s onLongClick, switch to getAbsoluteAdapterPosition() to get the correct item position, which helps avoid position calculation errors during updates:

@Override
public boolean onLongClick(View v) {
    clickListener.onItemLongClick(getAbsoluteAdapterPosition(), v);
    return true; // Return true to indicate we've handled the long click
}

Quick Check on Your Data Flow

Your data source logic looks solid (since SharedPreferences loads correctly on re-entry), but just double-check that the Adapter is always using the same offLimitsItemsList reference as your Activity. Since you’re passing the list directly to the Adapter constructor, this should be fine—but if you ever replace the entire list later, remember to call notifyDataSetChanged() or the appropriate granular update method.

Once you fix these issues, your RecyclerView should behave correctly: deleting items will update the view properly, and new items will show up without bringing back old deleted content.

内容的提问来源于stack exchange,提问作者RJB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:55:28