【问题标题】:Implementing Navigation Drawer实现导航抽屉
【发布时间】:2016-02-18 22:49:48
【问题描述】:

我正在尝试在我的应用程序中实现导航,但遇到了一些我似乎无法摆脱的问题。

当我尝试运行应用程序时,出现以下错误。我尝试更改片段的不同导入,我也尝试扩展活动以扩展片段,但我\这给出了更多错误并且不得不将其更改回 AppCompatActivity。

错误

Caused by: java.lang.ClassCastException: android.support.design.widget.CoordinatorLayout cannot be cast to android.support.v4.widget.DrawerLayout
02-18 14:31:11.535 16698-16698/com.example.rory.pocketchef E/AndroidRuntime:     at com.example.rory.pocketchef.MainActivity.onCreate(MainActivity.java:42)

实现导航抽屉的行

drawerFragment = (FragmentDrawer) getFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
    drawerFragment.setDrawerListener(this);

Main Activity XML,用于导航抽屉片段

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout">

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

   <fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="com.example.rory.pocketchef.FragmentDrawer"
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />
</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 请发布您的activity.xml。您的 DrawerLayout 似乎是 CoordinatorLayout...
  • 完成,如果有不同,我将使用标签视图进行不同的活动
  • 问题似乎出在这一行:setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), 您从CoordinatorLayoutDrawerLayout 添加了drawer_layoutDrawerLayout

标签: android android-fragments


【解决方案1】:

如果您想要DrawerLayout,请不要使用CoordinatorLayout。将您的 CoordinatorLayout 包装在 DrawerLayout 中:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- Your stuff -->

    </android.support.design.widget.CoordinatorLayout>

    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

更多信息可以在这里找到: http://developer.android.com/intl/es/training/implementing-navigation/nav-drawer.html

【讨论】:

  • 另外,在新的支持库中有一个好东西叫做NavigationView而不是ListView
  • 可以的,我只是从android训练中复制过来的,可以优化一下。
  • 感谢您的帮助!在我问另一个问题之前会更好地查看文档
【解决方案2】:

试试这个:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- Your stuff -->

在您的 java 中,添加 DrawerLayout 的 id 而不是 CoordinatorLayout

drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);

【讨论】:

  • 因此,主要活动的所有正常布局内容都应该放在嵌套的 CoordinatorLayout 中,然后我所拥有的导航片段将进入 DrawerLayout
  • 实际上,Fragments 应该在CoordinatorLayout 中,而使用DrawerLayout 的好处是,实现NavigationDrawer 所以,你的内容应该是CoordinatorLayout,因为我注意到在上面代码上的cmets。
  • 感谢您的帮助! +1
猜你喜欢
  • 2015-09-18
  • 1970-01-01
  • 1970-01-01
  • 2020-07-11
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多