我正在使用自定义可扩展列表适配器。当用户单击某个子项时,我会突出显示该子项。在用户打开/折叠组之前,这一切正常。假设用户触摸组 2 项目 1。这将突出显示组 2 项目 1。然后用户打开组 1。现在突出显示组 3 项目 2。我已经做了一些选择不同项目的测试,但找不到突出显示的行将跳转到的模式。有时它在列表中向上,有时它在列表中向下。我无法弄清楚放入活动的
onGroupExpandListener
和 onGroupCollapseListener
以重新突出显示正确视图的逻辑。有什么想法吗?
编辑:我的 onChildClickListener 中的当前代码
if (groupPosition == 0){
switch(childPosition) {
case 0:
previouslySelectedView.setBackgroundResource(R.color.transparent);
currentlySelectedView.setBackgroundResource(R.color.blue);
break;
所有团体/儿童使用相同的代码
以下是一些建议:
这就是我获取选定 ID 的方法(记住我使用多重选择模式):
final SparseBooleanArray checkedPositions = expList.getCheckedItemPositions();
final ExpandableListAdapter adapter = expList.getExpandableListAdapter();
List<Long> checkedIds = new ArrayList<Long>();
if (packedPositionType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
for (int i = checkedPositions.size() - 1; i >= 0; i--) {
if (checkedPositions.valueAt(i)) {
checkedIds.add(adapter.getGroupId(checkedPositions.keyAt(i)));
}
}
}
不过,就您而言,您需要检查是否有儿童挤满的位置。另请注意,我的适配器具有稳定(唯一)的 ID。如果您没有稳定的 ID,那么您可以依靠 ExpandableListView 的 getPackedPositionForChild() 方法并将打包位置存储为标记。
在 ExpandableListView 上执行以下操作:
步骤 1. 将选择模式设置为单一(可以在 xml 中完成,如 android:choiceMode = "singleChoice")
步骤 2. 使用选择器 xml 作为背景 (android:listSelector = "@drawable/selector_list_item")
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<item android:drawable="@android:color/holo_blue_bright" android:state_pressed="true"/>
<item android:drawable="@android:color/holo_blue_light" android:state_selected="true"/>
<item android:drawable="@android:color/holo_blue_dark" android:state_activated="true"/>
</selector>
第3步。在onChildClick()回调中调用expandableListView.setItemChecked(index,true)。
index 是子项的从 0 开始的索引,计算如下
第 1 组 [索引 = 0]
第 2 组 [索引 = 4]
第 3 组 [索引 = 8]
这是一个工作示例:
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
...
int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
parent.setItemChecked(index, true);
return true;
}
mDrawerExpandableList.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
if(ExpandableListView.getPackedPositionGroup(selectedFragmentId) == groupPosition) {
mDrawerExpandableList.setItemChecked(mDrawerExpandableList.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(groupPosition)), true);
}
}
});
mDrawerExpandableList.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
if(ExpandableListView.getPackedPositionGroup(selectedFragmentId) == groupPosition) {
mDrawerExpandableList.setItemChecked(((ExpandableListView)drawerListView).getFlatListPosition(selectedFragmentId), true);
}
}
});
//MY LIST A FOLLOWS
(0)SALES
(0)SALES
(1)SALES VIEW
(2)SALES RETURN
(3)SALES RETURN VIEW
(1)STOCK
(0)ADD STOCK
(1)VIEW STOCK
(2)SETTINGS
(3)NOTIFICATION
//declare tow integer variable globali as follows
private int lastCheckedGroupPosition=-1;
private int lastCheckedChildPosition=-1;
mDrawerList.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if(groupPosition==2){
lastCheckedGroupPosition=groupPosition;
lastCheckedChildPosition=-1;
int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(groupPosition));
parent.setItemChecked(index, true);
//TODO Write here the code for opening settings page
...............
...............
...............
return true;
}
else if(groupPosition==3){
lastCheckedGroupPosition=groupPosition;
lastCheckedChildPosition=-1;
int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(groupPosition));
parent.setItemChecked(index, true);
//TODO Write here the code for opening settings page
...............
...............
...............
return true;
}
return false;
}
});
mDrawerList.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if(groupPosition==0){
if(childPosition==0){
//TODO Write here the code for opening SALES page
...............
...............
...............
}
else if(childPosition==1){
//TODO Write here the code for opening SALES VIEW page
...............
...............
...............
}
else if(childPosition==2){
//TODO Write here the code for opening SALES RETURN page
...............
...............
...............
}
else if(childPosition==3){
//TODO Write here the code for opening SALES RETURN VIEW page
...............
...............
...............
}
}
else if(groupPosition==1){
if(childPosition==0){
//TODO Write here the code for opening ADD STOCK page
...............
...............
...............
}
else if(childPosition==1){
//TODO Write here the code for opening VIEW STOCK page
...............
...............
...............
}
}
int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition));
parent.setItemChecked(index, true);
lastCheckedGroupPosition=groupPosition;
lastCheckedChildPosition=childPosition;
return false;
}
});
mDrawerList.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
if(groupPosition==lastCheckedGroupPosition){
if(lastCheckedChildPosition!=-1){
int index = mDrawerList.getFlatListPosition(ExpandableListView.getPackedPositionForChild(lastCheckedGroupPosition, lastCheckedChildPosition));
mDrawerList.setItemChecked(index, true);
}
else{
int index = mDrawerList.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(lastCheckedGroupPosition));
mDrawerList.setItemChecked(index, true);
}
}
else{
if(mDrawerList.isGroupExpanded(lastCheckedGroupPosition)){
if(lastCheckedChildPosition!=-1){
int index = mDrawerList.getFlatListPosition(ExpandableListView.getPackedPositionForChild(lastCheckedGroupPosition, lastCheckedChildPosition));
mDrawerList.setItemChecked(index, true);
}
else{
int index = mDrawerList.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(lastCheckedGroupPosition));
mDrawerList.setItemChecked(index, true);
}
}
else{
mDrawerList.setItemChecked(-1, true);
}
}
}
});
mDrawerList.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
if(groupPosition==lastCheckedGroupPosition){
if(lastCheckedGroupPosition!=-1){
int index = mDrawerList.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(lastCheckedGroupPosition));
mDrawerList.setItemChecked(index, true);
}
else{
mDrawerList.setItemChecked(-1, true);
}
}
if(mDrawerList.isGroupExpanded(lastCheckedGroupPosition)){
if(lastCheckedChildPosition!=-1){
int index = mDrawerList.getFlatListPosition(ExpandableListView.getPackedPositionForChild(lastCheckedGroupPosition, lastCheckedChildPosition));
mDrawerList.setItemChecked(index, true);
}
else{
int index = mDrawerList.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(lastCheckedGroupPosition));
mDrawerList.setItemChecked(index, true);
}
}
else{
mDrawerList.setItemChecked(-1, true);
}
}
});
checkedGroupPosition
和
checkedChildPosition
中记录选中的组位置和子位置并更新
#onGroupExpand
和
#onGroupCollapse
中选中的项目来解决它:
override fun onGroupExpand(groupPosition: Int) {
// Re-check the previous checked item because the flat position may have changed.
updateCheckedItem()
}
override fun onGroupCollapse(groupPosition: Int) {
// Re-check the previous checked item because the flat position may have changed.
updateCheckedItem()
}
private fun updateCheckedItem() {
// Checked item is a child item.
if (checkedGroupPosition > -1 && checkedChildPosition > -1) {
if (expandableListView.isGroupExpanded(checkedGroupPosition)) {
// Check the item again if the group is expanded.
expandableListView.setItemChecked(
expandableListView.getFlatListPosition(
ExpandableListView.getPackedPositionForChild(checkedGroupPosition, checkedChildPosition)
),
true,
)
} else {
// Clear the checked item if its group is collapsed.
expandableListView.setItemChecked(-1, true)
}
return
}
// Checked item is a group item, check the item again.
if (checkedGroupPosition > -1) {
expandableListView.setItemChecked(
expandableListView.getFlatListPosition(
ExpandableListView.getPackedPositionForGroup(checkedGroupPosition)
),
true,
)
}
}