Android 如何在ExpandableListView中选择子级?
我已经问过这个问题,但没有成功。所以我再问一次(对不起,顺便说一句) 我仍然有这个问题: 让我继续。我的应用程序中存在这种情况: 我想对第二组的2dn项执行performClick()。 目前我所能做的就是使用以下代码行使用performClick()扩展第二个组:Android 如何在ExpandableListView中选择子级?,android,listview,expandablelistview,Android,Listview,Expandablelistview,我已经问过这个问题,但没有成功。所以我再问一次(对不起,顺便说一句) 我仍然有这个问题: 让我继续。我的应用程序中存在这种情况: 我想对第二组的2dn项执行performClick()。 目前我所能做的就是使用以下代码行使用performClick()扩展第二个组: mGattServicesList.performItemClick(mGattServicesList.getChildAt(1), 1, mGattServicesList.getItemIdAtPosition(1));
mGattServicesList.performItemClick(mGattServicesList.getChildAt(1), 1, mGattServicesList.getItemIdAtPosition(1));
知道
private ExpandableListView mGattServicesList;
是否有一种非常简单的方法可以对组中的项执行performClick()
我想这样做,因为我有一个类似listner的
private final ExpandableListView.OnChildClickListener servicesListClickListner =
new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
但我不想自己点击该项目,也找不到在该组中选择该特定项目的方法
提前感谢您列表中的特定项目不能有侦听器,但您可以随心所欲。只需检查
groupPosition
和childPosition
是否是您想要的,然后执行该操作(或其他适合其他项目的操作)
在ExpandableListView.OnChildClickListener中
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//groupPosition tells you what group the clicked child came from
//childPosition tells you what child was clicked
if (groupPosition == 2 && childPosition == 2) {
//so this code only executes if the 2nd child in the 2nd group is clicked
}
//you can ignore the other items or do something else when they are clicked
}
首先,
ExpandableListView
支持一种简单的方法来扩展您需要的组:
mGattServicesList.expandGroup(groupPosition);
//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()
。唯一的缺点是,您的子click listener也会收到一个空视图
//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);
最后,您可以调用performItemClick()
:
我应该先说一下,我没有用IDE检查这段代码,所以可能有一些语法错误会阻止编译。但不幸的是,总而言之,在以编程方式单击子视图时,应该传达不那么容易的步骤
最后请注意,您提供的图片显示组和子计数从1开始。请注意,它们实际上被视为零基位置。因此,第一个组位于位置0,每个组的第一个子组位于位置0。好的,但使用此解决方案,我仍然需要自己单击子组,对吗?是的。。我想在另一个函数中执行click(),这样它就可以调用onChildClick(),啊,我的误解。我有个主意。请稍等,谢谢你的回复!然而,getChildId(groupPosition,childPosition)方法似乎不存在,我在Yes中找不到其他有效的方法!!实际上,在行mGattServicesList.getChildId(groupPosition,childPosition)中;我将其更改为:mGattServicesList.getExpandableListAdapter().getChildId(1,2);而且做得很好,非常感谢:)很高兴能帮上忙。更新了我的答案以修复错误。
performItemClick(childToClick, flatPos, id);