【问题标题】:RecyclerView item selection is throwing errorRecyclerView 项目选择抛出错误
【发布时间】:2025-12-10 11:25:02
【问题描述】:

我尝试在 SelectionTracker 的帮助下实现 RecyclerView 中的项目选择功能,但得到 IllegalArgumentException 并且堆栈跟踪并不直观,因为它只显示元数据。

这就是我构建跟踪器的方式

wordAdapter.tracker = new SelectionTracker.Builder<Long>(
                "mySelectionId",
                recyclerView,
                new StableIdKeyProvider(recyclerView),
                new MyDetailsLookUp(recyclerView),
                StorageStrategy.createLongStorage()
        ).withSelectionPredicate(
                SelectionPredicates.<Long>createSelectAnything()
        ).build();

MyDetailsLookUp

class MyDetailsLookUp extends ItemDetailsLookup<Long> {
        RecyclerView recyclerView;

        MyDetailsLookUp(RecyclerView recyclerView) {
            this.recyclerView = recyclerView;
        }

        @Nullable
        @Override
        public ItemDetails<Long> getItemDetails(@NonNull MotionEvent e) {
            View view = recyclerView.findChildViewUnder(e.getX(), e.getY());
            if (view != null) {
                //  getting individual view holders
                return ((WordAdapter.MyViewHolder) recyclerView.getChildViewHolder(view)).getItemDetails();
            }
            return null;
        }
    }
WordAdapterViewHolder中的

getItemDetails方法

ItemDetailsLookup.ItemDetails<Long> getItemDetails(){
            return new ItemDetailsLookup.ItemDetails<Long>(){
                @Override
                public int getPosition() {
                    return getAdapterPosition();
                }

                @Nullable
                @Override
                public Long getSelectionKey() {
                    return getItemId();
                }
            };
        }

堆栈跟踪 1

java.lang.IllegalArgumentException
        at androidx.core.util.Preconditions.checkArgument(Preconditions.java:38)
        at androidx.recyclerview.selection.DefaultSelectionTracker.anchorRange(DefaultSelectionTracker.java:269)
        at androidx.recyclerview.selection.MotionInputHandler.selectItem(MotionInputHandler.java:60)
        at androidx.recyclerview.selection.TouchInputHandler.onLongPress(TouchInputHandler.java:132)
        at androidx.recyclerview.selection.GestureRouter.onLongPress(GestureRouter.java:96)
        at android.view.GestureDetector.dispatchLongPress(GestureDetector.java:778)
        at android.view.GestureDetector.-wrap0(Unknown Source:0)
        at android.view.GestureDetector$GestureHandler.handleMessage(GestureDetector.java:293)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

令人惊讶的是,我在 Kotlin 中实现了相同的代码,并且在那里运行良好。我发现了一个类似问题的 SO 帖子this,接受的答案建议传递自定义 ItemKeyProvider 而不是 StableIdKeyProvider。因此,这样做时会遇到这个错误。

堆栈跟踪 2

java.lang.IllegalStateException: Two different ViewHolders have the same stable ID. Stable IDs in your adapter MUST BE unique and SHOULD NOT change.
     ViewHolder 1:ViewHolder{987472c position=1 id=-1, oldPos=-1, pLpos:-1} 
     View Holder 2:ViewHolder{e50e5f5 position=2 id=-1, oldPos=-1, pLpos:-1 not recyclable(1)} androidx.recyclerview.widget.RecyclerView{617d73 VFED..... .F....ID 42,42-1038,1542 #7f08007b app:id/recyclerview}, adapter:com.example.roomwordssample.WordAdapter@7ada430, layout:androidx.recyclerview.widget.GridLayoutManager@a15b8a9, context:com.example.roomwordssample.Main2Activity@ca1d275
        at androidx.recyclerview.widget.RecyclerView.handleMissingPreInfoForChangeError(RecyclerView.java:4058)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3982)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3652)
        at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1877)
        at androidx.recyclerview.widget.RecyclerView$1.run(RecyclerView.java:407)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:655)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

更新的追踪器

wordAdapter.tracker = new SelectionTracker.Builder<Long>(
                "mySelectionId",
                recyclerView,
               new GetItemDetails(recyclerView, ItemKeyProvider.SCOPE_MAPPED),
                new MyDetailsLookUp(recyclerView),
                StorageStrategy.createLongStorage()
        ).withSelectionPredicate(
                SelectionPredicates.<Long>createSelectAnything()
        ).build();

CustomItemKeyProvider

class CustomItemKeyProvider extends ItemKeyProvider<Long> {
        RecyclerView recyclerView;

        CustomItemKeyProvider(RecyclerView recyclerView, int scope) {
            super(scope);
            this.recyclerView = recyclerView;
        }

        @Nullable
        @Override
        public Long getKey(int position) {
            return wordAdapter.getItemId(position);
        }

        @Override
        public int getPosition(@NonNull Long key) {
            RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForItemId(key);
            return viewHolder == null ? RecyclerView.NO_POSITION : viewHolder.getLayoutPosition();
        }
    }

P.SwordAdapter.setHasStableIds(true) 是在将适配器设置为 RecyclerView 之前完成的

【问题讨论】:

  • 由于您的适配器具有稳定的 id,它必须为每个适配器位置返回唯一的 ID。您现在如何返回该 ID?

标签: java android android-recyclerview


【解决方案1】:

在你的堆栈跟踪中:

java.lang.IllegalStateException: Two different ViewHolders have the same stable ID. Stable IDs in your adapter MUST BE unique and SHOULD NOT change.
     ViewHolder 1:ViewHolder{987472c **position=1 id=-1**, oldPos=-1, pLpos:-1} 
     View Holder 2:ViewHolder{e50e5f5 **position=2 id=-1**, oldPos=-1, pLpos:-1 not recyclable(1)} 

在上面的错误中,对于列表中的位置 1 和 2,您使用的是相同的 ID,但它必须是唯一的。为了与选择跟踪器一起使用,请确保您拥有唯一的 ID。 通知列表你有稳定的ID

setHasStableIds(true)

在 RecyclerView.Adapter 中,你需要确保你使用的是唯一的

public abstract class WordAdapter extends RecyclerView.Adapter {
  private List<Item> itemsList = new ArrayList<>();
  //other override methods

  @Override
    public long getItemId(int position) {
        return position; // here each value must be unique either position or data unique id
    }
}

【讨论】: