gpt4 book ai didi

android - ExpandableListview OnGroupClickListener 未触发

转载 作者:太空宇宙 更新时间:2023-11-03 13:26:55 32 4
gpt4 key购买 nike

我正在关注:Programmatically collapse a group in 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...
});
}

此替代方案的结果相同...有什么想法吗?

最佳答案

我建议您先检查导入(未在您的问题中显示),并确保您已导入 android.widget.ExpandableListView.OnGroupClickListener

或替换:new OnGroupClickListener() {

使用 新的 android.widget.ExpandableListView.OnGroupClickListener {

编辑:

我还注意到您在 boolean onGroupClick(...) 的正文中返回“true”,这意味着“点击已处理”并且组永远不会折叠、展开。

如果你想扩展你应该返回false。所以我建议你这样做:

expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
Log.d("onGroupClick:", "worked");

parent.smoothScrollToPosition(groupPosition);

if (parent.isGroupExpanded(groupPosition)) {
parent.collapseGroup(groupPosition);
} else {
parent.expandGroup(groupPosition);
}

return false;
}
});

关于android - ExpandableListview OnGroupClickListener 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18291612/

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