我正在关注这个:以编程方式折叠 ExpandableListView 中的组。我希望用户一次只能扩展一个组并平滑滚动到正确的位置。我编写了一个实现 OnGroupClickListener 的自定义适配器:
public class CategoriesListAdapter extends BaseExpandableListAdapter implements OnGroupClickListener {
private ExpandableListView expListView;
private int lastExpandedGroupPosition = -1;
// I get the expListView in the constructor...
@Override
public boolean onGroupClick(ExpandableListView parent, View view, int groupPosition, long id) {
parent.smoothScrollToPosition(groupPosition);
if (parent.isGroupExpanded(groupPosition)) {
parent.collapseGroup(groupPosition);
} else {
parent.expandGroup(groupPosition);
}
return true;
}
@Override
public void onGroupExpanded(int groupPosition) {
if (groupPosition != lastExpandedGroupPosition) {
expListView.collapseGroup(lastExpandedGroupPosition);
}
super.onGroupExpanded(groupPosition);
lastExpandedGroupPosition = groupPosition;
}
}
不幸的是,onGroupClick 事件从未被触发......(我在方法中放置了一些日志)。我也试过:
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View view, int groupPosition, long id) {
// Same as above...
});
}
这种替代方案的结果相同......有什么想法吗?