android - 使用 arne.jans 中的 ExpandableListFragment 永远不会触发 onListItemClickListener 事件

标签 android android-fragments expandablelistview

我想为我的 Fragment 使用 ExpandableListView,但由于它不是原生的,所以我使用了这个线程:https://stackoverflow.com/a/6193434/969881包含扩展 Fragment 的 ExpandableListFragment 类。

这很好用,我这样声明我的 fragment :

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 不是重写方法(例如:ExpandableListFragment 类型的方法 onChildClick(ExpandableListView, View, int, int, long) 必须重写父类(super class)方法)我想知道为什么因为我的类扩展ExpandableListView.OnChildClickListener。

  • onGroupCollapsed、onGroupExpand 和 onChildClick 也从未被触发,可能是因为它们未被视为覆盖父类(super class)方法的方法。

在此先感谢您的帮助!

最佳答案

找到了这个问题的答案。

我像这样使用 ExpandableListView 作为我的 fragment 的私有(private)参数:

private ExpandableListView elv;

然后,在创建我的 fragment 后,我将我的 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;
    }
});

关于android - 使用 arne.jans 中的 ExpandableListFragment 永远不会触发 onListItemClickListener 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9212651/

相关文章:

android - 同时运行两个 React Native 服务器

android - fragment : onCreateView is not called after fragment was added

c# - 适用于 Android 的 ExpandableListView Mono

android - 如何删除 TextView 的 9 补丁背景中的填充

android - 如何判断 'Mobile Network Data' 是启用还是禁用(即使通过 WiFi 连接)?

java - 通过替换 fragment 更新主要内容时出错

java - ExpandableListView 不调用 getGroupView 也不填充

Android ExpandableListView 如何保存展开/折叠状态?

android - Xamarin - Android - 启动画面(多个)

java - 在 ListFragment 的适配器显示的列表之前添加自定义布局