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,然后实现多选删除的例子。像这样:
这里写图片描述

首先长按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<String> 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<String> 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);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Spring Boot搭建的项目中使用Spring Task定时任务可能会遇到一些。在一篇介绍如何基于Spring Boot搭建的项目使用Spring Task定时任务的文章中,作者分享了一些常见的问题和解决方案。其中一些问题包括: 1. 定时任务不执行:有时候定时任务可能不会按预期执行。这可能是由于定时任务的配置出现了问题,比如忘记添加注解或配置错误的cron表达式。解决此问题的方法是仔细检查定时任务的配置,确保注解正确添加,并且cron表达式正确设置。 2. 并发执行问题:默认情况下,Spring Task定时任务是单线程执行的,可能会出现并发执行导致的问题。解决此问题的方法是使用线程池和多线程任务调度来实现并发执行,可以通过配置ThreadPoolTaskScheduler来实现。 3. 任务执行时间过长:如果定时任务的执行时间非常长,可能会导致后续任务无法及时执行。解决此问题的方法是使用异步执行或者将长时间执行的任务拆分为多个小任务。 以上是一些可能遇到的以及解决方案。如果你在项目中使用Spring Task定时任务时遇到其他问题,建议查阅官方文档或者参考相关的社区资源来获取更多的帮助和解决方案。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [SpringBoot系列(六):使用SpringBoot定时任务时不得不采的](https://blog.csdn.net/qq_44601070/article/details/121295823)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值