如何在ExpandableListView中选择子级? [英] How to select child in ExpandableListView?

查看:130
本文介绍了如何在ExpandableListView中选择子级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经问过这个问题,但没有成功.所以我再问一次(对不起,顺便说一句).

我仍然有这个问题:

如何访问ExpandableListView中的项目?.

让我继续.我的应用程序中存在这种情况:

我想对第二组的2dn项目执行performClick(). 目前,我所能做的就是使用以下代码行通过performClick()扩展第二个组:

 mGattServicesList.performItemClick(mGattServicesList.getChildAt(1), 1, mGattServicesList.getItemIdAtPosition(1));

知道

private ExpandableListView mGattServicesList;

有没有一种非常简单的方法可以对组中的项目执行performClick()?

我想这样做,因为我有一个像这样的列表器

private final ExpandableListView.OnChildClickListener servicesListClickListner =
        new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                                        int childPosition, long id) {

但是我不想自己单击该项目,也找不到在该组中选择此特定项目的方法.

提前谢谢

解决方案

首先,ExpandableListView支持一种扩展所需组的简便方法:

mGattServicesList.expandGroup(groupPosition);

以编程方式单击一个项目有些棘手.使用performItemClick()方法在正确的轨道上,但是在使用方法上还差一点.我将假设您没有使用标题.这进一步使事情复杂化.

首先,您需要获取要单击的视图.奇怪的是,这不是必需的.您可以使用空的View安全地调用performItemClick().唯一的缺点是您的孩子点击侦听器也将收到一个空视图.

//First we need to pack the child's two position identifiers
long packedPos = ExpandableListView.getPackedPositionForChild(int groupPosition, int childPosition);

//Then we convert to a flat position to use with certain ListView methods
int flatPos = mGattServicesList.getFlatListPosition(packedPos);

//Now adjust the position based on how far the user has scrolled the list.
int adjustedPos = flatPos - mGattServicesList.getFirstVisiblePosition();

//If all is well, the adjustedPos should never be < 0
View childToClick = mGattServicesList.getChildAt(adjustedPos);

现在,我们需要位置和ID来馈给performItemclick().您将看到步骤类似于检索视图.所以说真的,您不必再次输入此信息...而是显示您需要的内容:

//You can just reuse the same variables used above to find the View
long packedPos = ExpandableListView.getPackedPositionForChild(int groupPosition, int childPosition);
int flatPos = mGattServicesList.getFlatListPosition(packedPos);

//Getting the ID for our child
long id = mGattServicesList.getExpandableListAdapter().getChildId(groupPosition, childPosition);

最后,您可以调用您的performItemClick():

performItemClick(childToClick, flatPos, id);

我应该做一个序言,我没有针对IDE检查此代码,因此可能存在一些语法错误,这些错误会阻止编译.但是,不幸的是,总体而言,在编程方式单击子视图时,应该传达出并非那么简单的步骤.

最后一点,您提供的图片显示组和子级计数从1开始.请注意,它们实际上被认为是从零开始的位置.因此,第一个组的位置为0,每个组的第一个孩子的位置为0.

I already asked the question but with no success. So I'm asking again (sorry btw).

I still have this issue :

How to access items inside ExpandableListView?.

Let me resume. I have this situation in my app :

I want to make a performClick() on the 2dn item of the 2nd group. All I can do for the moment is to expand the second group with a performClick() using this line of code :

 mGattServicesList.performItemClick(mGattServicesList.getChildAt(1), 1, mGattServicesList.getItemIdAtPosition(1));

knowing

private ExpandableListView mGattServicesList;

Isn't there a very simple way to performClick() to the item inside a group ?

I want to do it because I have then a listner like

private final ExpandableListView.OnChildClickListener servicesListClickListner =
        new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
                                        int childPosition, long id) {

But I don't want to click to the item by myself and I don't find a way to select this specific item in this group.

Thank you in advance

解决方案

First off, the ExpandableListView supports an easy way to expand the group you need:

mGattServicesList.expandGroup(groupPosition);

To programmatically click an item is a little tricky. You're on the right track with using the performItemClick() method but you are a little off on how to use it. I will assume you are not using headers. That further complicates things.

First you need to obtain the View to be clicked. Oddly enough, this is not a requirement. You can safely invoke performItemClick() with a null View. The only downside being is that your child click listener will also receive a null view.

//First we need to pack the child's two position identifiers
long packedPos = ExpandableListView.getPackedPositionForChild(int groupPosition, int childPosition);

//Then we convert to a flat position to use with certain ListView methods
int flatPos = mGattServicesList.getFlatListPosition(packedPos);

//Now adjust the position based on how far the user has scrolled the list.
int adjustedPos = flatPos - mGattServicesList.getFirstVisiblePosition();

//If all is well, the adjustedPos should never be < 0
View childToClick = mGattServicesList.getChildAt(adjustedPos);

Now we need the position and id to feed to performItemclick(). You'll see the steps are similar to retrieving the View. So really, you don't have to further type this out again...but to show what you need:

//You can just reuse the same variables used above to find the View
long packedPos = ExpandableListView.getPackedPositionForChild(int groupPosition, int childPosition);
int flatPos = mGattServicesList.getFlatListPosition(packedPos);

//Getting the ID for our child
long id = mGattServicesList.getExpandableListAdapter().getChildId(groupPosition, childPosition);

Finally, you can invoke your performItemClick():

performItemClick(childToClick, flatPos, id);

I should preface, I didn't get to check this code against an IDE so there may be some syntax errors that'll prevent compilation. But all in all should convey the, unfortunately, not so easy steps in programmatically clicking a child view.

Final note, the pic you provided shows the group and child counts start at 1. Be aware that they are actually considered zero based positions. So the first group is at position 0 and the first child for each group is at position 0.

这篇关于如何在ExpandableListView中选择子级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆