错误充气类RecyclerView(使用DrawerLayout)

时间:2015-11-26 09:10:46

标签: android android-studio navigation-drawer

我有一个DrawerLayout片段, 然后我想在活动中为我的主要布局添加一个新的recyclerview但是 我卡住并得到错误充气类RecyclerView 试图在recyclerview标签的任何地方放置但仍然是 得到膨胀错误。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.juandirection.ActivityMapScreen">



    <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">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.design.widget.AppBarLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:theme="@style/AppTheme.AppBarOverlay">

                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:background="?attr/colorPrimary"
                        app:popupTheme="@style/AppTheme.PopupOverlay" />

                </android.support.design.widget.AppBarLayout>
            </RelativeLayout>
            <RecyclerView
                android:id="@+id/rvSettings"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </FrameLayout>

        <fragment
            android:id="@+id/fragment_navigation_drawer"
            android:name="com.juandirection.fragments.NavigationDrawerFragment"
            android:layout_width="@dimen/nav_dr"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="false"
            app:layout="@layout/fragment_navigation_drawer"
            tools:layout="@layout/fragment_navigation_drawer" />

    </android.support.v4.widget.DrawerLayout>


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

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

为什么你没有使用NavigationView,它很容易集成并隐藏片段的所有适配器复杂性。您希望mto实现的UI就可以完成。

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:fitsSystemWindows="true">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="0dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/home_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/menu_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>

这样你需要在活动的onCreate()方法中使用几行代码来处理导航抽屉

  private DrawerLayout mDrawerLayout;

public void onCreate(){

//do something

setUpNavigationDrawer();

}


    private void setUpNavigationDrawer() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(View drawerView) {

            }

            @Override
            public void onDrawerClosed(View drawerView) {

            }

            @Override
            public void onDrawerStateChanged(int newState) {

            }
        });

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        if (navigationView != null) {
            setupDrawerContent();
        }
    }


    private void setupDrawerContent() {   
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        switch (menuItem.getItemId()) {

                            case R.id.nav_home:
                                break;
                            case R.id.first_item:
                            //do some stuff
                                mDrawerLayout.closeDrawers();                           
                                break;
                            case R.id.second_item:
                                startActivity(new Intent(getApplicationContext(), FlyersActivity.class));
                                overridePendingTransition(R.anim.right_in, R.anim.left_out);
                                break;
                            default:
                                mDrawerLayout.closeDrawers();
                        }
                        mDrawerLayout.closeDrawers();
                        return true;
                    }
                });
        }

您可以从菜单文件 navigation_drawer_menu.xml 中填充菜单,该文件应放在资源目录的Menu目录中。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_action_home"
            android:title="Home"
            android:checkable="false"/>
        <item
            android:id="@+id/first_item"
            android:icon="@drawable/ic_action_bell"
            android:title="Notification"
            android:checkable="false" />
</group>       
</menu>

希望这可以帮助您实现所需的UI,并根据Google指南进行最新操作。