0

我有一個片段,它將呈現一個帶有浮動動作按鈕的recycleview。問題在於,當列表爲空時,晶圓廠保持在右上方,當列表中有少量項目時,晶圓廠一直保持在列表的下方,但不在最下方,並且當列表填滿與項目屏幕。請有人幫助我嗎?貝婁是我的代碼:Android浮動動作按鈕並沒有固定在底部

<?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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.app.juninho.financeapp.activity.FuncionarioActivity"> 

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

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/btn_add_func" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|right|end" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_margin="16dp" 
     android:src="@drawable/ic_menu_send" 
     app:layout_behavior="com.app.juninho.financeapp.utils.ScrollAwareFABBehavior" /> 

if the list is empty, fab keeps at the top, wich is wrongif the list has a few items, fab doesn't keep totaly at the bottom

Here is the correct way, fab is at the bottom

Using RelativeLayout it seems to work, but its too at the right/bottom corner

+0

問題是最有可能在這個自定義類:com.app.juninho.financeapp.utils.ScrollAwareFABBehavior – BladeCoder

+0

Hi BladeCoder!感謝您的回答!這個自定義類只是在清單上隱藏了工廠,並在冷靜下來時顯示出來。我試圖刪除這個,但問題仍然存在... –

+0

你能用一個Framelayout/RelativeLayout替換CoordinatorLayout嗎? –

回答

0

的問題是你的FAB屬性:app:layout_behavior="com.app.juninho.financeapp.utils.ScrollAwareFABBehavior"

#。您不需要在FAB中加入此項。只要刪除此屬性並將其屬性app:layout_anchor="@id/funcionario_recycler_view"app:layout_anchorGravity="bottom|right|end"添加到FABanchor它與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" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.app.juninho.financeapp.activity.FuncionarioActivity"> 

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

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/btn_add_func" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|right|end" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_margin="16dp" 
     android:src="@drawable/ic_menu_send" 
     app:layout_anchor="@id/funcionario_recycler_view" 
     app:layout_anchorGravity="bottom|right|end" /> 
</android.support.design.widget.CoordinatorLayout> 

#。如果你想隱藏/顯示FAB滾動RecyclerView的時候,那麼你就可以在你的java代碼做pragmatically如下:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.funcionario_recycler_view); 
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.btn_add_func); 

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
{ 
    @Override 
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) 
    { 
     if (dy > 0 ||dy<0 && fab.isShown()) 
     { 
      fab.hide(); 
     } 
    } 

    @Override 
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) 
    { 
     if (newState == RecyclerView.SCROLL_STATE_IDLE) 
     { 
      fab.show(); 
     } 

     super.onScrollStateChanged(recyclerView, newState); 
    } 
}); 

希望這將有助於〜

+0

RecyclerView不會移動,FAB應該停留在佈局的右下角,因此不需要錨點。 – BladeCoder

+0

是的,你是對的。佈局錨點在這裏沒有必要。 – FAT

+0

嗨,大家好!不幸的是,它沒有奏效。我即將放棄它......它保持着同樣的行爲。我忘了說我正在使用這個rec​​ycleview,並且這個晶圓廠有一個片段,它在onViewCreate中設置了recycleview適配器,並且我沒有使用小吃棒。我的應用只有一個活動,其中包含一個導航抽屜和FrameLayout,以顯示我的所有片段的佈局...還有其他我可以做的事情嗎?非常感謝你的幫助! –