2016-05-14 192 views
1

我想用RecyclerView和Toolbar創建簡單的CoordinatorLayout,但與標準解決方案的不同之處在於工具欄應該位於底部,並且在RecyclerView滾動到底部時應該消失。CoordinatorLayout底部的工具欄

因此,我創建簡單的佈局:

當然
<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/main_content" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar_bottom" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/recycler_view"/> 

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

中的工具欄在頂部而不是底部。我該如何解決這個問題?

回答

3

哇,這是一個很酷的想法,不知道這一點,但你可以嘗試這樣做

<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/main_content" 
    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.v7.widget.RecyclerView 
     android:id="@+id/my_list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </android.support.v7.widget.RecyclerView> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar_bottom" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom"/> 
    </android.support.design.widget.CoordinatorLayout> 

但老實說,我不認爲這就足夠了。

也許解決方案是給Toolbar定製app:layout_behavior,正如您通常對FAB所做的那樣,正如我在my blog article中所做的那樣。

請讓我知道結果如何!

編輯

我剛剛意識到,也許你正在尋找的是新引進的bottom navigation bar! 我看到有很多第三方庫可以做到這一點,或者嘗試實現它自己的!

+0

不幸的是,這是不可能用你的解決方案,因爲我得到ClassCastException異常,在XML第二件事應該是RecyclerView。 – ThirdMartian

+0

哦,如果我記得好了CoordinatorLayout是FrameLayout的一個子類,你可以在xml塊中交換元素,而不會產生副作用。請參閱我的編輯! – fiipi

+0

哦,我錯了,這是暫時的錯誤,現在工具欄是在底部,我試圖找到滾動隱藏/顯示的解決方案。我不想使用底部導航欄,因爲工具欄上的項目應該打開新的活動,而不是在容器中更換新的片段。我的意思是,已經爲底部導航欄創建的庫對於我的解決方案來說太大了,但也許我將不得不使用它們。 – ThirdMartian

0

嘗試用這種

<android.support.design.widget.CoordinatorLayout 
android:id="@+id/main_content" 
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/id_appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fitsSystemWindows="true"> 

      <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar_bottom" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      app:layout_scrollFlags="scroll|enterAlways"/> 

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

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/my_list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" > 
    </android.support.v7.widget.RecyclerView> 


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

請記住,我想要在底部的工具欄! – ThirdMartian

+0

在工具欄中使用此工具欄android:layout_gravity =「bottom」 – Masum

+0

工具欄仍位於頂部 – ThirdMartian