如何在ExpandableListView组内单击按钮时更改片段?

时间:2017-03-11 00:57:34

标签: android android-fragments expandablelistview

我在ExpandableListView内显示了以下Fragment。当用户点击突出显示的箭头时,更改Fragment的正确方法是什么?另外,如何获得单击按钮的组索引(即突出显示箭头的索引0)?

enter image description here

1 个答案:

答案 0 :(得分:0)

使用ExpandableListView.OnGroupClickListener

实现侦听器并单击它时调用的方法是:

 public preloadImages(imgUrls: string[]) {
        console.log(this.cachedResources); //Everything is fine here
        return Observable.from(imgUrls).concatMap(url=> Observable.of(this.getImage(url));
    }

如果您想要更精细的控制,还可以实现听众ExpandableListView.OnGroupCollapseListenerExpandableListView.OnGroupExpandListener

要实现它,声明一个实现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
}

然后,从你的Fragment视图中获取你的ExpandableListView(你可能会这样做,我只是在这里猜测,如果不是那样的话):

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
        }
});

最后,将侦听器添加到ExpandableListView:

ExpandableListView listView = 
    (ExpandableListView) this.getView()
   .findViewById(R.id.whatever_you_set_your_expandablelistview_id_to);

你的听众" onGroupClick"每当点击一个组时都会被调用。请注意,您可能只能为每个ExpandableListView添加一个侦听器。 Also, be careful of Context leaks when using anonymous classes for listeners on views!