android - 带有 ListView 和自定义 Header 的 NavigationDrawer fragment

标签 android android-layout android-fragments android-listview android-navigation

我有一个问题,我无法解决。在我的应用程序中,我有一个抽屉导航,定义如下:

activity_main.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions. -->
<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->
<fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"  />

在 MainActivity 类上,我通常通过 setContentView 传递此 xml: MainActivity.java

setContentView(R.layout.activity_main);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
        getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);

在我的 NavigationDrawerFragment onCreateView 上,我通常用这个来膨胀它的 xml:

NavigationDrawerFragment.java

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View root = (View)inflater.inflate(R.layout.fragment_navigation_drawer,null,false);
    mDrawerListView = (ListView)root.findViewById(R.id.drawer_list_view);

    mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    });

这是 fragment_navigation_drawer.xml: fragment_navigation_drawer.xml

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="10dp">

    <ImageView
            android:id="@+id/profile_picture"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentLeft="true"/>

    <TextView
            android:id="@+id/user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/profile_picture"
            android:textStyle="bold"
            android:textSize="15sp" />

    <TextView
            android:id="@+id/user_mail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/user_name"
            android:layout_alignLeft="@id/user_name"
            android:fontFamily="sans-serif-light" />


    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:src="@android:drawable/ic_menu_close_clear_cancel"/>

</RelativeLayout>

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:choiceMode="singleChoice"
          android:id="@+id/drawer_list_view"
          android:dividerHeight="0dp"
          android:background="@color/navigation_drawer_background" />

当我运行代码时,我在 MainActivity 的 setContentView 上得到一个异常:

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

我认为 NavigationDrawer 布局有问题。我做错了什么?

编辑:我将布局更改为:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
<!-- The navigation drawer -->
<FrameLayout
        android:id="@+id/left_drawer_frame"
        android:layout_width="@dimen/navigation_drawer_dimension"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"/>

所以对于 FrameLayout,在我的 Activity 中我使用:

mFragment = new Fragment();
        getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content_frame, mFragment)
            .commit();

然后我用这个给 fragment 充气:

View root = inflater.inflate(R.layout.fragment_main, container, false);

一切正常

最佳答案

迟到总比不到好。

查看您的代码(变量名称、布局结构等),我假设您使用的是 Android Studio,并且您使用 Android Studio“创建项目”向导提供的“抽屉导航”模板创建了项目。

如果上述假设成立,那么你的问题出在NavigationDrawerFragmentonCreateView。您必须从 onCreateView 返回膨胀 View ,在这种特殊情况下您应该返回的是 root 而不是 mDrawerListView

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View root = (View)inflater.inflate(R.layout.fragment_navigation_drawer,null,false);
    mDrawerListView = (ListView)root.findViewById(R.id.drawer_list_view);

    mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    });

    return root;

    // This is wrong
    // return mDrawerListView;
}

这是我们在编辑 IDE 生成的代码时经常发生的错误。

希望这对您有所帮助。

关于android - 带有 ListView 和自定义 Header 的 NavigationDrawer fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22554074/

相关文章:

android - 禁用在editText android中输入

java - 什么时候可以在android中使用强引用并且这段代码会泄漏吗?

android - Fragment 的 GetTag 返回空对象引用

android - 为什么 android 在 looper 等待时处理 ANR 中的应用程序

android - java.lang.NoClassDefFoundError : androidx/core/view/ViewCompat when using FlexboxLayout 错误

java - 初始屏幕填满整个智能手机屏幕,但不裁剪

android - 管理 fragment ,公共(public)静态 viewpager

android - 应用请求未声明的权限 - "Test access to protected storage"

android - 相机 2 Api..java.lang.IllegalArgumentException : Surface had no valid native Surface

android - IBM Worklight 应用程序无法在 Xcode 中运行