如何在Recyclerview SelectionTracker上正确设置泛型?

时间:2019-06-21 13:16:36

标签: java android generics android-recyclerview

我有一个Recyclerview,正在实现SelectionTracker。我有一个警告:

Unchecked assignment: 'androidx.recyclerview.selection.ItemDetailsLookup.ItemDetails' to 'androidx.recyclerview.selection.ItemDetailsLookup.ItemDetails<java.lang.Long>'

我似乎无法解决。如果我从ItemDetailsLookup类中删除<Long>,则警告将移至SelectionTracker Builder。我在下面附加了我的代码。

这些是我覆盖的类:


    class ListItemSelectionPredicate extends SelectionTracker.SelectionPredicate<Long> {
        @Override
        public boolean canSetStateForKey(@NonNull Long key, boolean nextState) {
            return selectionEnabled;
        }

        @Override
        public boolean canSetStateAtPosition(int pos, boolean nextState) {
            return selectionEnabled;
        }

        @Override
        public boolean canSelectMultiple() {
            return true;
        }
    }

    class ListItemKeyProvider extends ItemKeyProvider<Long> {

        private RecyclerView mRecyclerView;

        ListItemKeyProvider(RecyclerView recyclerView) {
            super(ItemKeyProvider.SCOPE_MAPPED);

            mRecyclerView = recyclerView;
        }

        @Override
        public Long getKey(int pos) {
            return listAdapter.getItemId(pos);
        }

        @Override
        public int getPosition(@NonNull Long key) {
            ListAdapter.ViewHolder viewHolder
                    = (ListAdapter.ViewHolder) mRecyclerView.findViewHolderForItemId(key);

            return viewHolder.getLayoutPosition();
        }
    }

    class ListItemDetailsLookup extends ItemDetailsLookup<Long> {

        private RecyclerView mRecyclerView;

        ListItemDetailsLookup(RecyclerView recyclerView) {
            mRecyclerView = recyclerView;
        }

        @Override
        public ItemDetails<Long> getItemDetails(@NonNull MotionEvent e) {
            View view = mRecyclerView.findChildViewUnder(e.getX(), e.getY());

            return view == null
                    ? null
                    : ((ListAdapter.ViewHolder) mRecyclerView.getChildViewHolder(view)).getItemDetails();
        }
    }

警告在此行:

            return view == null
                    ? null
                    : ((ListAdapter.ViewHolder) mRecyclerView.getChildViewHolder(view)).getItemDetails();

这是我对SelectionTracker的实例化-在onCreate中构建一次。


        listTracker = new SelectionTracker.Builder<>(
                "list-item-selection"
                , listRecycler
                , new StableIdKeyProvider(listRecycler)
                , new ListItemDetailsLookup(listRecycler)
                , StorageStrategy.createLongStorage())
                .withSelectionPredicate(
                        new ListItemSelectionPredicate()
                )
                .build();

这是我的getItemDetails方法,它嵌套在我的viewHolder类中。


        ItemDetailsLookup.ItemDetails<Long> getItemDetails() {
            return new ItemDetailsLookup.ItemDetails<Long>() {

                @Override
                public int getPosition() {
                    return getAdapterPosition();
                }

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

有关类型转换的警告(我认为是?)应该不会发生。

0 个答案:

没有答案