gpt4 book ai didi

android - 如何在 ExpandableListView 中选择 child ?

转载 作者:太空宇宙 更新时间:2023-11-03 12:06:40 25 4
gpt4 key购买 nike

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

我还有这个问题:

How to access items inside ExpandableListView? .

让我继续。我的应用程序中有这种情况:

enter image description here

我想在第 2 组的第 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。奇怪的是,这不是必需的。您可以使用空 View 安全地调用 performItemClick()。唯一的缺点是您的子点击监听器也会收到空 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);

现在我们需要将位置和 ID 提供给 performItemclick()。您会看到这些步骤类似于检索 View 。所以实际上,您不必再进一步输入...但要显示您需要的内容:

//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 检查此代码,因此可能存在一些语法错误,这些错误会阻止编译。但总而言之,不幸的是,以编程方式单击 subview 并不是那么简单的步骤。

最后请注意,您提供的图片显示组和子项计数从 1 开始。请注意,它们实际上被视为基于零的位置。所以第一组在位置 0,每个组的第一个 child 在位置 0。

关于android - 如何在 ExpandableListView 中选择 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28328010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com