【发布时间】:2017-03-11 00:57:34
【问题描述】:
我在Fragment 中显示了以下ExpandableListView。当用户单击突出显示的箭头时,更改Fragment 的正确方法是什么?另外,我如何获得被点击按钮的组索引(即突出显示的箭头的索引 0)?
【问题讨论】:
标签: android android-fragments expandablelistview
我在Fragment 中显示了以下ExpandableListView。当用户单击突出显示的箭头时,更改Fragment 的正确方法是什么?另外,我如何获得被点击按钮的组索引(即突出显示的箭头的索引 0)?
【问题讨论】:
标签: android android-fragments expandablelistview
使用ExpandableListView.OnGroupClickListener。
实现监听并点击时调用的方法是:
void onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
// int position and other parameters give you everything you need to know about the click
}
如果您想要更精细的控制,还可以实现侦听器 ExpandableListView.OnGroupCollapseListener 和 ExpandableListView.OnGroupExpandListener。
要实现它,请声明一个实现 OnGroupClickListener 的对象,或者创建一个匿名类:
ExpandableListView.OnGroupClickListener mListener =
new ExpandableListView.OnGroupClickListener({
public void onGroupClick(ExpandableListView parent, View v, int groupPosition, long id){
//handle whatever you want to do on the click in this method
}
});
然后,从 Fragment 视图中获取 ExpandableListView(你可能会这样做,不过我只是在这里猜测,如果不是这样也没关系):
ExpandableListView listView =
(ExpandableListView) this.getView()
.findViewById(R.id.whatever_you_set_your_expandablelistview_id_to);
最后,将监听器添加到您的 ExpandableListView:
listView.setOnGroupClickListener(mListener);
当一个组被点击时,你的监听器的“onGroupClick”就会被调用。请注意,您可能只能为每个 ExpandableListView 添加一个侦听器。 Also, be careful of Context leaks when using anonymous classes for listeners on views!
【讨论】: