ExpandableListview OnGroupClickListener没有发射 [英] ExpandableListview OnGroupClickListener Not Firing
问题描述
我跟随这名:<一href=\"http://stackoverflow.com/questions/4314777/programmatically-collapse-a-group-in-expandablelistview\">Programmatically在倒塌 ExpandableListView一组。我希望能够在用户在同一时间展开只有一组,顺利滚动到合适的位置。我已经写了一个实现OnGroupClickListener自定义适配器:
I'm following this: Programmatically collapse a group in ExpandableListView. I want the user capable to expand only one group at a time and smoothly scroll to the right position. I've written a custom adapter that implements 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事件从来没有发射。(我已经把一些日志的方法内)。我也试过:
Unfortunately the onGroupClick event is never fired...(I've put some logs inside the method). I've also tried:
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View view, int groupPosition, long id) {
// Same as above...
});
}
对于这种替代的结果相同...任何想法?
Same results for this alternative...any ideas?
推荐答案
我会建议您检查进口第一个(在你的问题未显示),并确保您已导入 android.widget .ExpandableListView.OnGroupClickListener
I would suggest you to check for the imports first (not shown in your question),and make sure you have imported android.widget.ExpandableListView.OnGroupClickListener
或更换:新OnGroupClickListener(){
与新android.widget.ExpandableListView.OnGroupClickListener {
编辑:
我也注意到,您在布尔onGroupClick(...)
,表示点击被处理的身体返回真和组决不会倒塌,扩大了。
I also noticed that you are returning 'true' at the body of boolean onGroupClick(...)
that means "the click was handled" and groups will never be collapsed, expanded.
如果你想expand.So,我建议你做这样你应该返回false:
You should return false if you want to expand.So I suggest you to do like this :
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;
}
});
这篇关于ExpandableListview OnGroupClickListener没有发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!