2015-08-27 4 views
9

Ich kann nicht die Floating Action Button (FAB) in der richtigen Position angezeigt werden. Ich möchte, dass es zwischen der Kopfzeile und dem ersten Element in meiner Navigationsleiste erscheint.Floating Action Button (FAB) kann nicht zwischen Header und Elementen in DrawerLayout mit RecyclerView platziert werden

enter image description here

Zeit, ich habe es in der unteren rechten Ecke des Headers zu erscheinen und nicht oben auf die Linie zwischen den ersten und zweiten Elementen (ersten Elemente = Kopf & zweites Elemente = erster Punkt in Recyclerview).

Meine App die folgenden appcompat Artikel verwendet:

  • appcompat-v7: 23.0.0
  • recyclerview-v7: 23.0.0
  • Design: 23.0.0

Ich verwende eine Nav-Schublade, aber ich kann die NavigationView nicht verwenden, da ich die Elementeinträge anpassen muss und kein einfaches Menü laden muss.

Wie Sie wissen, ist die Schublade wirklich nicht 2 verschiedene Kontrollen. Der Header ist eigentlich das '0' Element in der RecyclerView. Ich weiß nicht, ob das einen Unterschied macht.

Hier ist mein aktuelles xml für den Header/"0 Ansicht 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> 

Ich glaube, ich könnte die FAB an der falschen Stelle/Datei. Hier ist das Xml für die Schublade.

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

HILFE !!!!!

+0

Normalerweise sollten Sie nicht FAB Platz in einer Schublade. FAB wird für eine geförderte positive Aktion verwendet. Aber Schublade ist für die Navigation bestimmt! – makata

+0

Einverstanden. Ich denke, dass es in dieser Situation angemessen ist. Ich möchte ein Optionsmenü (von Art) platzieren. Ich könnte es auf den Boden der Schublade hinzufügen, aber ich denke, es sieht besser aus und macht mehr Sinn als ein FAB. Irgendwelche Ideen, wie es geht? – JustLearningAgain

+0

Es gibt keine Möglichkeit, dass eine Ansicht außerhalb einer Ansicht liegt, in der sie sich befindet. Ich denke, Sie müssten Ihre Kopfzeile außerhalb der Recycleransicht in ein 'AppBarLayout' verschieben. – tachyonflux

Antwort

7

Beispiel Schublade Fragment Layout Ihrer vorhandenen 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> 
+1

Schöne Lösung! Für 'FAB' empfehle ich' layout_margin' als '16dp' für Telefone und' 24dp' für Tablets zu verwenden –

Verwandte Themen