Android ExpandableListView长组点击监听防止展开

发布于 2025-01-08 09:31:21 字数 713 浏览 0 评论 0原文

我在 Android 应用程序中使用 ExpandableListView,希望在用户长时间单击组元素时执行操作,因此我在 BaseExpandableListAdapter< 中定义了一个 OnLongClickListener /代码> 扩展名。侦听器按方面工作,但子元素不再扩展。有什么想法吗?

 public class ConnectionAdapter extends BaseExpandableListAdapter {
    ...
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,  
                             ViewGroup parent) {
        // convertView is a LinearLayout
        convertView.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View v) {
                // my action here
                return true;
            }
        });
     }
     ...   
 }

I use a ExpandableListView in my Android application an want to perfrom an action if the user clicks long on the group element, so I defined a OnLongClickListener in my BaseExpandableListAdapter extention. The listener works as aspected but the child elements does not expand anymore. Any ideas?

 public class ConnectionAdapter extends BaseExpandableListAdapter {
    ...
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,  
                             ViewGroup parent) {
        // convertView is a LinearLayout
        convertView.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View v) {
                // my action here
                return true;
            }
        });
     }
     ...   
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始终不够 2025-01-15 09:31:21

您可以在可扩展列表视图上设置 setOnItemLongClickListener。
ExpandableListView.PACKED_POSITION_TYPE_GROUP是一个组的id,将其更改为
ExpandableListView.PACKED_POSITION_TYPE_CHILD,您可以通过长按组子项进行操作。

像这样的东西:

    pager_income = (ExpandableListView) findViewById(R.id.income_scroll);

    pager_income.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
                // Your code with group long click 

                return true;
            }

            return false;
        }
    });

You can set setOnItemLongClickListener on your expandablelistview.
ExpandableListView.PACKED_POSITION_TYPE_GROUP is the id of a group, change it to
ExpandableListView.PACKED_POSITION_TYPE_CHILD and you can manipulate with longclicks on group childs.

Something like that:

    pager_income = (ExpandableListView) findViewById(R.id.income_scroll);

    pager_income.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
                // Your code with group long click 

                return true;
            }

            return false;
        }
    });
浮生面具三千个 2025-01-15 09:31:21

您的代码没有进一步处理任何其他“onClick”事件的原因是您在返回中传递了“true”。如果您指示事件已处理,操作系统将停止尝试进一步处理任何进一步的事件。要让它处理此事件并展开,那么您需要将返回更改为 false 而不是 true

The reason that your code is not further processing any other 'onClick' events is because you are passing a 'true' in your return. If you indicate that an event was handled, the OS stops attempting to further process any further events. To have it process THIS event, and also expand, then you need to change the return to false instead of true

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文