当我长按时,Recyclerview-selection 有时会选择 2 个项目

问题描述

我使用 StaggeredGridLayoutManager 作为 LayoutManager 创建了一个回收视图。

感谢 recyclerview-selection 库,我想实现多项选择,但是当我长按某个项目时,它有时会选择 2。(我正在使用 DB 来检索数据)


这是我的主要代码:

GridRecyclerView notesRecyclerView = binding.noteRecyclerView;
notesRecyclerView.setHasFixedSize(true);

adapter = new NoteAdapterTest();
notesRecyclerView.setAdapter(adapter);

SelectionTracker<Long> tracker = new SelectionTracker.Builder<>("mySelection",notesRecyclerView,new StableIdKeyProvider(notesRecyclerView),new NoteDetailsLookup(notesRecyclerView),StorageStrategy.createLongStorage())
                .withSelectionPredicate(SelectionPredicates.createSelectAnything()).build();

adapter.setSelectionTracker(tracker);

我的适配器:


    private SelectionTracker<Long> selectionTracker;
    private static final DiffUtil.ItemCallback<Note> DIFF_CALLBACK = new DiffUtil.ItemCallback<Note>() {
        @Override
        public boolean areItemsTheSame(@NonNull Note oldItem,@NonNull Note newItem) {
            return oldItem.getId() == newItem.getId();
        }

        @Override
        public boolean areContentsTheSame(@NonNull Note oldItem,@NonNull Note newItem) {
            return oldItem.getTitle().equals(newItem.getTitle()) && oldItem.getContent().equals(newItem.getContent());
        }
    };

    public NoteAdapterTest() {
        super(DIFF_CALLBACK);
        setHasStableIds(true);
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {
        AdapterNoteBinding binding = AdapterNoteBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false);
        return new NoteHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder,int position) {
        Note note = getItem(position);
        ((NoteHolder) holder).bind(note,selectionTracker.isSelected((long) position));
    }


    public static class NoteHolder extends RecyclerView.ViewHolder  {

        private final AdapterNoteBinding binding;

        public NoteHolder(@NonNull AdapterNoteBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public final void bind(Note note,boolean isActive) {
            Context context = binding.getRoot().getContext();
            itemView.setActivated(isActive);

            if (isActive) {
                binding.noteAdapterLayout.getBackground().setTint(context.getColor(R.color.blue));

                
            } else {
                binding.noteTitle.setText(note.getTitle());
                
            }
        }


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

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

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


    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setSelectionTracker(SelectionTracker<Long> selectionTracker) {
        this.selectionTracker = selectionTracker;
    }

    }

我的查找:

public class NoteDetailsLookup extends ItemDetailsLookup<Long> {

    private final RecyclerView recyclerView;

    public NoteDetailsLookup(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) {
             RecyclerView.ViewHolder viewHolder = recyclerView.getChildViewHolder(view);
             if (viewHolder instanceof NoteAdapterTest.NoteHolder) {
                 return ((NoteAdapterTest.NoteHolder) viewHolder).getItemDetails();
             }
         }
         return null;
     }
 }

解决方法

您怎么知道选择了 2 个项目?选择跟踪器返回什么?
尝试始终在您的视图持有者中设置背景色调:

if (isActive) {
    binding.noteAdapterLayout.getBackground().setTint(context.getColor(R.color.blue));
} else {
    binding.noteAdapterLayout.getBackground().setTint(context.getColor(R.color.white)); // your default color
}

这可能只是一个视图状态问题。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...