Search code examples
androidkotlinnavigation-drawerdrawerlayout

FragmentContainerView as the navigation view in DrawerLayout


I have a fragment that I sometimes want to display as a full screen and sometimes as the contents of the burger menu.

I have setup an activity with a burger menu like so:

<?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/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ui.SomeActivity"
    >
<!--    tools:openDrawer="left"-->

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/my_menu_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="com.example.myapplication.ui.WhateverFragment"
        android:background="@color/someBackgroundColor"
        tools:layout="@layout/brand_feed_fragment"
        android:clickable="true" />
<!-- clickable had no effect actually -->
    .... activity UI here

</androidx.drawerlayout.widget.DrawerLayout>

The WhateverFragment has a RecyclerView and in each item I add a OnClickListener. When presented as a fragment of full screen activity that listener is invoked.

However, when i tap anywhere in my 'drawer' then it simply closes and my onclicklistener is not invoked in my fragment.

What am I missing?


Solution

  • As per @Mike-M:

    The drawer should be listed last within the <DrawerLayout> tags in order for it to receive touch events properly. That is, move your <androidx.fragment.app.FragmentContainerView> to after everything in activity UI here.