How to use RecyclerView in Navigation Drawer in Android

In this android programming source code example, we are going to use RecyclerView in Navigation Drawer in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to use RecyclerView in Navigation Drawer in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_below="@id/toolbar">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Navigation Drawer"
            android:gravity="center_horizontal|center_vertical"/>

        <android.support.design.widget.NavigationView
            android:id="@+id/shitstuff"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <include
                    android:id="@+id/header"
                    layout="@layout/drawer_header" />
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/list"
                    android:layout_width="280dp"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/header"/>
            </RelativeLayout>
        </android.support.design.widget.NavigationView>
    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

MainActivity.java

import android.content.Context;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class NavigationViewActivity11 extends AppCompatActivity{
    private DrawerLayout mDrawerLayout;
    private RecyclerView list;
    private adapter recyclerAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_view11);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        list = (RecyclerView) findViewById(R.id.list);
        //Data
        ArrayList<String> nav_item = new ArrayList<>();
        nav_item.add("Home");
        nav_item.add("App");
        nav_item.add("Blog");

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        list.setLayoutManager(layoutManager);
        recyclerAdapter = new adapter(NavigationViewActivity11.this, nav_item);
        list.setAdapter(recyclerAdapter);

        ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name, R.string.app_name);

        mDrawerLayout.addDrawerListener(mDrawerToggle);

        mDrawerToggle.syncState();

    }

    private class adapter extends RecyclerView.Adapter<adapter.myViewHolder> {
        Context context;
        List<String> mData;

        public adapter(Context context, List<String> data) {
            this.context = context;
            this.mData = data;
        }

        @Override
        public adapter.myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.navigationrecyclerview_adapter11, parent, false);
            return new myViewHolder(view);
        }

        @Override
        public void onBindViewHolder(adapter.myViewHolder holder, int position) {
            holder.country.setText(mData.get(position));
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
        public class myViewHolder extends RecyclerView.ViewHolder {
            TextView nav;

            public myViewHolder(View itemView) {
                super(itemView);
                nav = (TextView) itemView.findViewById(R.id.nav);
            }
        }
    }

}

navigationrecyclerview_adapter11.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/nav"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="10dp"
        android:lineSpacingMultiplier="0.6"
        android:layout_margin="10dp"
        android:text="Item"
        android:textColor="#000000"
        android:textSize="15sp"
        />


</RelativeLayout>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.