0

I want to add navigation drawer in my activity that has a recycler view

If you need other xml file codes, tell me to put it here. MainPageActivity.kt

class MainPageActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener,CustomAdapter.OnItemClickListener {

    lateinit var toolbar:androidx.appcompat.widget.Toolbar
    lateinit var drawerLayout: DrawerLayout
    lateinit var navView:NavigationView

    private var data = arrayListOf<UserModel>()
    private lateinit var myadapter:CustomAdapter
    private val gson = Gson()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_page)

        toolbar =findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)

        drawerLayout = findViewById(R.id.drawer_layout)
        navView = findViewById(R.id.nav_view)

        val toggle = ActionBarDrawerToggle(this,drawerLayout,toolbar,0,0)
        drawerLayout.addDrawerListener(toggle)
        toggle.syncState()
        navView.setNavigationItemSelectedListener(this)


        val recyclerview = findViewById<RecyclerView>(R.id.recycler_view)

        recyclerview.addItemDecoration(SimpleDividerItemDecoration(this)) // for insert line divider to recyclerview items

    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.nav_profile->{
                Toast.makeText(this,"profile clicked",Toast.LENGTH_SHORT).show()
            }
            R.id.nav_message->{
                Toast.makeText(this,"message clicked",Toast.LENGTH_SHORT).show()
            }
            R.id.nav_frinds->{
                Toast.makeText(this,"frinds clicked",Toast.LENGTH_SHORT).show()
            }
            R.id.nav_update->{
                Toast.makeText(this,"update clicked",Toast.LENGTH_SHORT).show()
            }
            R.id.nav_logout->{
                Toast.makeText(this,"logout clicked",Toast.LENGTH_SHORT).show()
            }
        }
        drawerLayout.closeDrawer(GravityCompat.START)
        return true
    }

}  

activity_main_page.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
    tools:openDrawer="start">

    <include
        layout="@layout/content_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:drawable/screen_background_light_transparent"
        tools:listitem="@layout/item_list" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_menu" />


</androidx.drawerlayout.widget.DrawerLayout>  

I went ahead to create this navigation drawer according to the tutorials on YouTube Because I am a novice in the field of Android, no matter how hard I tried, I did not find the cause of the problem thank you

1
  • Can you include your nav_header.xml and nav_menu.xml? Commented Aug 19, 2020 at 9:22

1 Answer 1

1

Well you are adding recyclerview in the drawerlayout here. This layout consists of two layouts which are included. One is content_main , this is the back screen layout and the other one is NavigationView with id nav_view which is for the sliding drawer layout. What you can try doing is adding the recylerview in the nav_view and you can add the menu options in the recyclerview. I haven't tried it but inside nav_view is where you can aim your efforts.

Sign up to request clarification or add additional context in comments.

1 Comment

That's nice Edy, do share your solution here so that anyone in future can find that helpful as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.