android – 使用ListView和自定义标题的NavigationDrawer片段

前端之家收集整理的这篇文章主要介绍了android – 使用ListView和自定义标题的NavigationDrawer片段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我遇到了问题,我无法解决.在我的应用程序中,我有一个导航抽屉定义如下:

activity_main.xml中

  1. <android.support.v4.widget.DrawerLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/drawer_layout"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent" >
  7.  
  8. <!-- As the main content view,the view below consumes the entire
  9. space available using match_parent in both dimensions. -->
  10. <FrameLayout
  11. android:id="@+id/container"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent" />
  14.  
  15. <!-- android:layout_gravity="start" tells DrawerLayout to treat
  16. this as a sliding drawer on the left side for left-to-right
  17. languages and on the right side for right-to-left languages.
  18. If you're not building against API 17 or higher,use
  19. android:layout_gravity="left" instead. -->
  20. <!-- The drawer is given a fixed width in dp and extends the full height of
  21. the container. -->
  22. <fragment android:id="@+id/navigation_drawer"
  23. android:layout_width="@dimen/navigation_drawer_width"
  24. android:layout_height="match_parent"
  25. android:layout_gravity="start" />

在MainActivity类中,我通常使用setContentView传递这个xml:
MainActivity.java

  1. setContentView(R.layout.activity_main);
  2.  
  3. mNavigationDrawerFragment = (NavigationDrawerFragment)
  4. getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);

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

NavigationDrawerFragment.java

  1. public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
  2. View root = (View)inflater.inflate(R.layout.fragment_navigation_drawer,null,false);
  3. mDrawerListView = (ListView)root.findViewById(R.id.drawer_list_view);
  4.  
  5. mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  6. @Override
  7. public void onItemClick(AdapterView<?> parent,View view,int position,long id) {
  8. selectItem(position);
  9. }
  10. });

这是fragment_navigation_drawer.xml:
fragment_navigation_drawer.xml

  1. <LinearLayout
  2. android:orientation="vertical"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. xmlns:android="http://schemas.android.com/apk/res/android">
  6.  
  7. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:padding="10dp">
  11.  
  12. <ImageView
  13. android:id="@+id/profile_picture"
  14. android:layout_width="50dp"
  15. android:layout_height="50dp"
  16. android:layout_alignParentLeft="true"/>
  17.  
  18. <TextView
  19. android:id="@+id/user_name"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_marginLeft="10dp"
  23. android:layout_toRightOf="@id/profile_picture"
  24. android:textStyle="bold"
  25. android:textSize="15sp" />
  26.  
  27. <TextView
  28. android:id="@+id/user_mail"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:layout_below="@id/user_name"
  32. android:layout_alignLeft="@id/user_name"
  33. android:fontFamily="sans-serif-light" />
  34.  
  35.  
  36. <ImageView
  37. android:layout_width="wrap_content"
  38. android:layout_height="wrap_content"
  39. android:layout_alignParentRight="true"
  40. android:src="@android:drawable/ic_menu_close_clear_cancel"/>
  41.  
  42. </RelativeLayout>
  43.  
  44. <ListView xmlns:android="http://schemas.android.com/apk/res/android"
  45. xmlns:tools="http://schemas.android.com/tools"
  46. android:layout_width="match_parent"
  47. android:layout_height="match_parent"
  48. android:choiceMode="singleChoice"
  49. android:id="@+id/drawer_list_view"
  50. android:dividerHeight="0dp"
  51. android:background="@color/navigation_drawer_background" />

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

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

我认为NavigationDrawer布局有问题.我究竟做错了什么?

编辑:我将我的布局更改为:

  1. <android.support.v4.widget.DrawerLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/drawer_layout"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <!-- The main content view -->
  7. <FrameLayout
  8. android:id="@+id/content_frame"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"/>
  11. <!-- The navigation drawer -->
  12. <FrameLayout
  13. android:id="@+id/left_drawer_frame"
  14. android:layout_width="@dimen/navigation_drawer_dimension"
  15. android:layout_height="match_parent"
  16. android:layout_gravity="start"
  17. android:background="@android:color/white"/>

所以使用FrameLayout,在我的活动中我使用:

  1. mFragment = new Fragment();
  2. getSupportFragmentManager()
  3. .beginTransaction()
  4. .replace(R.id.content_frame,mFragment)
  5. .commit();

我用这个膨胀片段:

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

这一切似乎都有效

解决方法

迟到总比不到好.

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

如果上述假设为真,那么您的问题就在于NavigationDrawerFragment的onCreateView.你必须从onCreateView返回膨胀的视图,在这种特殊情况下你应该返回的是root而不是mDrawerListView

  1. public View onCreateView(LayoutInflater inflater,long id) {
  2. selectItem(position);
  3. }
  4. });
  5.  
  6. return root;
  7.  
  8. // This is wrong
  9. // return mDrawerListView;
  10. }

当我们编辑IDE生成代码时,这是一个常见的错误.

希望这可以帮助.

猜你在找的Android相关文章