Click系列的一些坑

这篇文章主要讲下面这两点:

  • longClick和Click事件冲突
  • multipleSelect如何处理

longClick和Click事件冲突

我们实现一个view的点击事件一般都要设置onClickListener(), 类似要实现view的长按事件,则要设置onLongClickListener(),但是有时候会发现longclick的效果会被click所覆盖,这是因为:

onLongClick() - This returns a boolean to indicate whether you have consumed the event and it should not be carried further. That is, return true to indicate that you have handled the event and it should stop here; return false if you have not handled it and/or the event should continue to any other on-click listeners.

意思是我们在写onLongClick()方法的时候,结果的返回值应该设置为true,这样表示这个时间onLongClick()已经处理好了,return false,表示这个时间还没有处理好,需要继续向下传递,所以会出现longclick()被覆盖的情况。

multipleSelect如何处理

有时候我们希望实现多选的item的情况,比如经常在APP中我们能看到长按一个Item,然后实现多选删除的例子。像这样:
Click系列的一些坑_第1张图片

首先长按Item,选择一个或多个item,然后点击标题栏上的菜单,就可以触发相应的点击事件,这种处理方式已经很流行,现在已被大多数用户所接受,那么该如何实现呢?
很好的例子,大家可以基础这个基础的例子改编出自己的风格的长按所选事件。

下面是关于上面图片,我的Demo:

private class MultipleSelectionListener implements AbsListView.MultiChoiceModeListener {
        AbsListView absListView;
        MainAdapter mainAdapter;

        public MultipleSelectionListener(AbsListView absListView, MainAdapter mainAdapter){
            this.absListView = absListView;
            this.mainAdapter = mainAdapter;
        }

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            int checkCount = absListView.getCheckedItemCount();
            mode.setTitle(checkCount + " Selected");
            mainAdapter.toggleSelection(position);
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.getMenuInflater().inflate(R.menu.main_select, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            toolbar.setVisibility(View.GONE);
            mainAdapter.showMultipleImage();
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            actionMode = mode;
            switch (item.getItemId()) {
                case R.id.delete:
                    //change the view and delete the selected notes from SQLite
                    SparseBooleanArray sparseBooleanArray = mainAdapter.getSparseBooleanArray();
                    for (int i = sparseBooleanArray.size() - 1; i >= 0; i--) {
                        if (sparseBooleanArray.valueAt(i)) {
                            NoteDateStructure note = mainAdapter.getItem(sparseBooleanArray.keyAt(i));
                            notes.remove(note);
                            DataBaseManager.getInstance(getApplicationContext()).deleteNote(note.ID);
                            mainAdapter.notifyDataSetChanged();
                        }
                    }
                    setViewMode();
                    //close CAB
                    mode.finish();
                    return true;
                case R.id.collect:
                    //move select book into a collections
                    DialogFragment dialogFragment = new MoveDialog();
                    //prepare for the list to display in the move dialog
                    ArrayList books = DataBaseManager.getInstance(getApplicationContext()).getBookList();
                    books.remove(toolbarName);
                    String[] names = new String[books.size()];
                    for(int i = 0; i < books.size(); i++){
                        names[i] = books.get(i);
                    }
                    //prepare for the selected items
                    List list = new ArrayList<>();
                    SparseBooleanArray sparse = mainAdapter.getSparseBooleanArray();
                    for (int i = sparse.size() - 1; i >= 0; i--) {
                        if (sparse.valueAt(i)) {
                            NoteDateStructure note = mainAdapter.getItem(sparse.keyAt(i));
                            list.add(note.ID);
                        }
                    }
                    String[] IDS = new String[list.size()];
                    for(int i = 0; i < list.size(); i++){
                        IDS[i] = list.get(i);
                    }
                    Bundle bundle = new Bundle();
                    bundle.putStringArray("BOOKS", names);
                    bundle.putString("OLD", toolbarName);
                    bundle.putStringArray("IDS", IDS);
                    dialogFragment.setArguments(bundle);
                    dialogFragment.show(getSupportFragmentManager(), "missiles");

                    return true;
                default:
                    return false;
            }
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mainAdapter.removeSelected();
            toolbar.setVisibility(View.VISIBLE);
        }
    }

你可能感兴趣的:(Click系列的一些坑)