簡體   English   中英

Recyclerview-selection select 有時長按2項

[英]Recyclerview-selection select sometimes 2 items when I long click

我創建了一個使用 StaggeredGridLayoutManager 作為 LayoutManager 的 recyclerview。

由於 recyclerview-selection 庫,我想實現多項選擇,但是當我長按某個項目時,它有時會選擇 2。(我正在使用數據庫來檢索數據)


這是我的主要代碼:

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
}

這可能只是一個視圖 state 問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM