使用SelectionTracker删除RecycleView中的项目

时间:2019-08-01 14:27:25

标签: java android android-recyclerview

我有一个RecyclerView,并且正在实现SelectionTracker。在我的RecyclerView的每个项目中,都有一个带OnClickListener的imageview要删除。选择SelectionTracker项的状态时,不会触发要删除的OnClickListener。

enter image description here

  1. 我的SelectionTracker构建器:

    SelectionTracker<Long> selectionTracker = new SelectionTracker.Builder(
                "macro-item-selection-tracker",
                mRecyclerView,
                new MacroItemRecyclerViewAdapter.PositionKeyProvider(mMacroItemsAdapter),
                new MacroItemDetailsLookup(mRecyclerView),
                StorageStrategy.createLongStorage())
                .withSelectionPredicate(SelectionPredicates.createSelectSingleAnything())
                .build();
    
  2. 删除点击监听器:

    public class DeleteMacroItemClickListener implements View.OnClickListener {
          @Override
          public void onClick(View view) {
              MacroItemViewHolderAction actions = getViewHolderActionSafety(view);
              actions.handleClickDeleteMacroItem(mMacroItem);
          }
    }
    
  3. inSelectionHotspot用于识别点击区域图像的删除:

    @Override
    public boolean inSelectionHotspot(@NonNull MotionEvent e) {
         Rect rect = new Rect();
         mDeleteMacroItem.getDrawingRect(rect);
         int[] locationOnScreen = new int[2];
         mDeleteMacroItem.getLocationOnScreen(locationOnScreen);
         if (((e.getRawX() > locationOnScreen[0] && (e.getRawX() < locationOnScreen[0] + mDeleteMacroItem.getWidth())))
                            && (e.getRawY() > locationOnScreen[1] && (e.getRawY() < locationOnScreen[1] + mDeleteMacroItem.getHeight()))) {
                return false;
          } else {
                return true;
          }
    //     return !rect.contains((int) e.getX(), (int) e.getY());
    }
    

1 个答案:

答案 0 :(得分:0)

观察者添加到您的选择跟踪器。

赞:

selectionTracker?.addObserver(object : SelectionTracker.SelectionObserver<Long>(){
    override fun onSelectionChanged() {
        super.onSelectionChanged()
        //onClick for that ImageView
    }
})