如何处理 ExpandableListView 子级的一行中不同视图的单击事件

发布于 2025-01-04 20:53:18 字数 444 浏览 1 评论 0原文

我知道如何通过订阅(setOnClickChildListener)ExpandableListView(或覆盖ExpandableListActivity回调方法:onChildClick)来处理事件。两者让我知道了 groupPosition、childPosition 和行布局视图(我的子级自定义布局)的详细信息。 但我想知道单击了哪个特定视图(例如我的自定义行的哪个文本视图)。

我尝试重写 mSimpleCursorTreeAdapter 的 getView 方法并将我的文本视图订阅到 onClick 侦听器。但这样我就无法知道点击的子行的groupPosition和childPosition。如果我以这种方式处理视图的 onClick,该事件将被消耗,并且 OnClickChildListener 和回调方法 onChildClick 永远不会被调度。

我想我说错了什么。我希望有人告诉我一个方法。

谢谢。

I know how to handle the events by subscribing (setOnClickChildListener) of the ExpandableListView (or overriding the ExpandableListActivity callback method: onChildClick). The two let me know the details groupPosition, childPosition and the view of the row layout (my custom layout for the childs).
But I want to know what particular view has been clicked (for example which textview of my custom row).

I tried it overriding the getView method of the mSimpleCursorTreeAdapter and subscribing my textviews to the onClick listener. But in this way I can't know the groupPosition and the childPosition of the child row clicked. If I handle the onClick of the views in this way, the event is consumed and OnClickChildListener and the callback method onChildClick never will be dispatched.

I guess I am saying something wrong. I hope someone tells me a way to do.

Thank you.

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

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

发布评论

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

评论(1

仅此而已 2025-01-11 20:53:18

我已经找到解决办法了:

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.row_child,
                    parent, false);
        }

        TextView textOne = (TextView) row
                .findViewById(R.id.text_row_child1);
        TextView textTwo = (TextView) row
                .findViewById(R.id.text_row_child2);

        HandleOnClick handleOnClick = new HandleOnClick (
                groupPosition, childPosition, getChildId(groupPosition,
                        childPosition));
        textOne.setOnClickListener(handleOnClick);
        textTwo.setOnClickListener(handleOnClick);

        return super.getChildView(groupPosition, childPosition,
                isLastChild, row, parent);
    }

    private class HandleOnClick implements OnClickListener {
        private int groupPosition;
        private int childPosition;
        private Long id;

        public HandleOnClick (int groupPostion, int childPosition,
                Long id) {
            this.groupPosition = groupPostion;
            this.childPosition = childPosition;
            this.id = id;
        }

        public void onClick(View v) {

            idChild = id; 
            switch (v.getId()) {

            case R.id.text_row_child1:


                break;


            case R.id.text_row_child2:

                //........

谢谢

I've found a solution:

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.row_child,
                    parent, false);
        }

        TextView textOne = (TextView) row
                .findViewById(R.id.text_row_child1);
        TextView textTwo = (TextView) row
                .findViewById(R.id.text_row_child2);

        HandleOnClick handleOnClick = new HandleOnClick (
                groupPosition, childPosition, getChildId(groupPosition,
                        childPosition));
        textOne.setOnClickListener(handleOnClick);
        textTwo.setOnClickListener(handleOnClick);

        return super.getChildView(groupPosition, childPosition,
                isLastChild, row, parent);
    }

    private class HandleOnClick implements OnClickListener {
        private int groupPosition;
        private int childPosition;
        private Long id;

        public HandleOnClick (int groupPostion, int childPosition,
                Long id) {
            this.groupPosition = groupPostion;
            this.childPosition = childPosition;
            this.id = id;
        }

        public void onClick(View v) {

            idChild = id; 
            switch (v.getId()) {

            case R.id.text_row_child1:


                break;


            case R.id.text_row_child2:

                //........

Thanks

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