【问题标题】:How to get listview from fragment?如何从片段中获取列表视图?
【发布时间】:2015-04-03 14:14:36
【问题描述】:

onCreateView 无法加载 listView

 mDrawerListView = (ListView) getActivity().findViewById(R.id.navigation_list);

在上面的代码中

mDrawerListView 为空对象

这是fragment_navation_drawer.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/drawer_layout"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#cccc"
              android:choiceMode="singleChoice"
              android:divider="@android:color/transparent"
              android:id="@+id/navigation_list"
              android:dividerHeight="0dp"/>

</LinearLayout>

如果我不使用 LinearLayout 而只使用 ListView

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="#cccc"
                  android:choiceMode="singleChoice"
                  android:divider="@android:color/transparent"
                  android:id="@+id/navigation_list"
                  android:dividerHeight="0dp"/>

喜欢这个xml并使用

mDrawerListView = (ListView) inflater.inflate(
                R.layout.fragment_navigation_drawer, container, false);

此代码有效

如何从片段线性布局调用listview?

像这样修改代码

mDrawerListView = (ListView) inflater.inflate(
                R.layout.fragment_navigation_drawer, container, false).findViewById(R.id.navigation_list);

这可以调用listView但其他错误

Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

【问题讨论】:

标签: android android-fragments android-listview android-view android-lifecycle


【解决方案1】:

假设mDrawerListViewFragmentView 层次结构中的ListView(而不是导航抽屉),错误的原因是FragmentView 层次结构有尚未创建。

FragmentonCreateView()中,替换

mDrawerListView = (ListView) getActivity().findViewById(R.id.navigation_list);

mDrawerListView = (ListView) rootView.findViewById(R.id.navigation_list);

进一步考虑:

如果您改用ListFragment,则可以通过调用getListView()(在onCreateView() 返回之后)直接获取对ListView 的引用。您甚至不需要使用这种方法来扩充 View,只需在 super() 构造函数中传递列表行布局并调用 setListAdapter()

编辑:

对于IllegalStateException 错误,问题在于您将View 视为孩子,但View 已经有父母。问题在这里解决:

1. ViewPager with a ListView fragment.

2. Fragments - The specified child already has a parent..

【讨论】:

  • 什么是“rootView”?这个 rootView 是什么时候创建的?
  • rootView 是从FragmentonCreateView() 的XML 布局扩展而来的View
  • 使用inflater调用根视图并成功调用listview但出现其他错误:(指定的孩子已经有一个父母。
  • ((ViewGroup)mDrawerListView.getParent()).removeView(mDrawerListView);这个代码工作谢谢!
【解决方案2】:

下面的代码对我有用。

ListView listView  = getListView();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                    Log.e("onItemClick", "onItemClick");
                }
            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    相关资源
    最近更新 更多