Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't place Floating Action Button (FAB) between header and items in DrawerLayout using RecyclerView

I can't get the Floating Action Button (FAB) to appear in the correct position. I want it to appear between the header and the first item in my nav drawer.

enter image description here

Currently, I've got it to appear in the bottom right corner of the header and NOT on top of the line between the 1st and 2nd elements (1st element = header & 2nd element = first item in recyclerview).

My app is using the following appcompat items:

  • appcompat-v7:23.0.0
  • recyclerview-v7:23.0.0
  • design:23.0.0

I'm using a nav drawer but I can't use the NavigationView because I need to customize the item entries and not load a simple menu.

As you know, the drawer is really not 2 different controls. The header is actually the '0' element in the RecyclerView. I don't know if this makes a difference.

Here is my current xml for the header/"0 view in 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"
    android:layout_width="match_parent"
    android:layout_height="@dimen/navdrawer_image_height">


    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/navDrawerHeaderView"
        android:layout_width="match_parent"
        android:layout_height="@dimen/navdrawer_image_height">

        <ImageView
            android:id="@+id/navdrawer_image"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/navdrawer_image_height"
            android:contentDescription="@string/cd_navdrawer_image"
            android:scaleType="centerCrop"
            android:src="@drawable/bg_material_design" />

        <de.hdodenhof.circleimageview.CircleImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/app_image"
            android:layout_width="@dimen/navdrawer_user_picture_size"
            android:layout_height="@dimen/navdrawer_user_picture_size"
            android:src="@drawable/ic_launcher"
            android:layout_marginTop="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            app:border_width="2dp"
            app:border_color="#FF000000"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/appNameTextView"
            android:text="App Name"
            android:textStyle="bold"
            android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginBottom="16dp"
            android:layout_alignParentBottom="true"
            android:textColor="@android:color/white"/>

    </RelativeLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/button_account"
        app:layout_anchor="@id/navDrawerHeaderView"
        app:layout_anchorGravity="bottom|right|end"
        app:elevation="4dp"/>

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

I think I might have the FAB in the wrong location/file. Here is the xml for the drawer.

     <?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"
    android:id="@+id/drawerLayout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true">

    <!-- Content layout -->
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/tool_bar"/>

        <FrameLayout
            android:id="@+id/contentFrame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/init_background">
        </FrameLayout>

    </LinearLayout>

    <!-- Pages -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#ffffff"
        android:scrollbars="vertical"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true">

    </android.support.v7.widget.RecyclerView>

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

HELP!!!!!

like image 336
JustLearningAgain Avatar asked Aug 27 '15 02:08

JustLearningAgain


1 Answers

example drawer fragment layout containing your existing RecyclerView:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="200dp">
        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#f00"
            android:id="@+id/header"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:scrollbars="vertical"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right|end"
        android:layout_margin="5dp"
        android:clickable="true"/>

</android.support.design.widget.CoordinatorLayout>
like image 94
tachyonflux Avatar answered Oct 16 '22 01:10

tachyonflux