-2

I have a DrawerLayout Fragment, I then I want to add a new recyclerview for my main layout in the activity but
Im stuck and getting Error inflating Class RecyclerView.
Tried to place anywhere in the XML the recyclerview tag but still
getting Inflating error.

<?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>
Charles Galvez
  • 1,100
  • 5
  • 19
  • 41

2 Answers2

0

Why your are not using NavigationView which is realy easy to integrate and hides all the adapter complexity fro fragment. The UI you want mto achieve is just can be done as.

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

And by this you need following few lines of code in activity's onCreate() method for working the Navigation drawer

  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;
                    }
                });
        }

And you can inflate the menu from menu file navigation_drawer_menu.xml which should be placed in Menu directory of resource directory.

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

Hope this will help you out the acheive the UI you need and also latest as per Google guidelines .

Mahendra Chhimwal
  • 1,810
  • 5
  • 21
  • 33