我想为我的Fragment使用ExpandableListView,但由于它不是原生的,我使用了这个线程:https://stackoverflow.com/a/6193434/969881包含一个扩展Fragment的ExpandableListFragment类。
这很好用,我声明我的片段是这样的:
public class MyExpListFragment extends ExpandableListFragment {
@Override
public void onCreate(Bundle savedInstanceState){
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(
getActivity().getApplicationContext(),
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { NAME, IS_EVEN },
new int[] { android.R.id.text1, android.R.id.text2 }
);
setListAdapter(mAdapter);
}
...
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("TAG", "Item selected");
}
}
但不幸的是,onListItemClick永远不会被触发。你有什么线索吗?
我也有这些问题:
onGroupCollapsed,onGroupExpand和onChildClick不是Overrided方法(例如:ExpandableListFragment类型的onChildClick方法(ExpandableListView,View,int,int,long)必须覆盖超类方法)我想知道为什么因为我的类扩展ExpandableListView.OnChildClickListener。
onGroupCollapsed,onGroupExpand和onChildClick也不会被触发,可能是因为它们不被视为覆盖超类方法的方法。
提前感谢您的帮助!
答案 0 :(得分:1)
找到了这个答案。
我使用ExpandableListView作为我的片段的私有参数,如下所示:
private ExpandableListView elv;
然后,在创建片段之后,我将elv链接到我的expandableListFragment创建的当前ExpandableListView:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
elv = getExpandableListView();
}
然后我可以触发我的ExpandableListView的任何事件:
elv.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
Log.d("TAG", "Group number " + groupPosition + " is clicked ");
return false;
}
});
答案 1 :(得分:0)
在我的ExpandableListFragment超类中添加了这些方法
@Override
public void onActivityCreated (Bundle savedInstanceState) {
Log.d("LocalListFragment", "onActivityCreated");
super.onContentChanged();
super.onActivityCreated(savedInstanceState);
}
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Map<String, String> childmap = (Map<String, String>) mAdapter.getChild(groupPosition, childPosition);
String c = (String)childmap.get(CHILD);
Map<String, String> groupmap = (Map<String, String>) mAdapter.getGroup(groupPosition);
String g = (String)groupmap.get(GROUP);
Log.d("LocalListFragment", "onChildClick c="+c+" g="+g);
return false;
}
似乎运作良好
仅供参考,
private static final String GROUP = "GROUP";
private static final String CHILD = "CHILD";
创建像这样的curGroupMap.put(GROUP,“GROUP”+ i); 的地图时使用