2

進出口使用MaterialDrawer與,我替換每個片段內容器FrameLayout根據所選項目FAB衝突,但我想添加一個FAB(只是爲了這個片段),與CoordinatorLayout所以交互可以處理很酷的動畫。片段佈局與CoordinatorLayout

MainDrawer佈局:

<?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="match_parent"> 

    <android.support.design.widget.AppBarLayout 
    android:id="@+id/appBar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fitsSystemWindows="true" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?actionBarSize" 
     android:minHeight="?actionBarSize" 
     android:theme="@style/Toolbar" 
     app:layout_scrollFlags="scroll|enterAlways" /> 

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

    <FrameLayout 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

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

我如何替換每個片段:

try { 
    supportFragmentManager.beginTransaction().replace(R.id.container, FeedFragment()).commit() 
} catch (e: IllegalStateException) { 
    Timber.i(e, "Fragment is still there.") 
} 

片段佈局:

<FrameLayout 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:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.RecyclerView 
    android:id="@+id/recyclerView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scrollbarAlwaysDrawVerticalTrack="true" 
    android:scrollbars="vertical" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:listitem="@layout/feed_item" /> 

    <android.support.design.widget.FloatingActionButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|right" 
    android:layout_marginBottom="@dimen/spacing_medium" 
    android:layout_marginRight="@dimen/spacing_medium" 
    android:elevation="@dimen/elevation_little" 
    app:fabSize="normal" 
    app:layout_anchor="@+id/container" 
    app:layout_anchorGravity="bottom|right" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    app:srcCompat="@drawable/ic_add_white_18dp" /> 

</FrameLayout> 

但作爲導致的FAB按鈕後面底部欄:

Sample

我試着設置協調佈局attribues(layout_anchor那些)相匹配的主要佈局ID,但它是不工作... 爲什麼?

+0

直接孩子,我有同樣的問題。你修好了嗎? –

+1

是的,你需要在主佈局中有FAB,應該是協調員佈局的直接子工作 – Caipivara

回答

1

定義活動內的浮動操作按鈕 - 目前該片段的容器被視爲父項。

FAB應該是CoordinatorLayout

+0

我不想做這個事件的BC,我將不得不打電話給碎片 – Caipivara

+0

您的FloatingActionButton可以充氣/構造超出你的片段。只要您引用FAB所在的上下文 - 您可以保持相同的功能。在你的片段中,創建一個成員變量'FloatingActionButton mFAB;'並在onCreateView中初始化它:'mFAB =(FloatingActionButton)container.findViewById(R.id.floating_action_button);' – apelsoczi