0

我試圖只在Recycler View位置處於頂部時顯示浮動操作按鈕。無論如何,我可以做到這一點?隱藏Android中的浮動操作按鈕,除非Recycler View中的位置位於頂部

這是我目前的佈局文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:background="#BBB" 
    android:layout_height="match_parent"> 

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

    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_margin="16dp" 
     android:src="@drawable/icon_post" /> 

</RelativeLayout> 

感謝:]

+0

http://stackoverflow.com/questions/31617398/floatingactionbutton-hide-on-list-scroll的可能的複製。 –

+0

我嘗試過使用該功能,但我認爲使用回收站視圖而不是列表視圖 – Lunkie

+1

嘗試此操作:https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when -list-is-scrolling%28part3%29/ –

回答

-1

感謝丹尼爾找到這個鏈接: https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling%28part3%29/ 解決我的問題

添加此晶圓廠按鈕,您的協調佈局:

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fabButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="end|bottom" 
    android:layout_margin="@dimen/fab_margin" 
    android:src="@drawable/ic_favorite_outline_white_24dp" 
    app:borderWidth="0dp" 
    app:layout_behavior="pl.michalz.hideonscrollexample.ScrollingFABBehavior" /> 

這個類添加到項目中:

public class ScrollingFABBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> { 
    private int toolbarHeight; 

    public ScrollingFABBehavior(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.toolbarHeight = Utils.getToolbarHeight(context); 
    } 

    @Override 
    public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton fab, View dependency) { 
     return dependency instanceof AppBarLayout; 
    } 

    @Override 
    public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) { 
     if (dependency instanceof AppBarLayout) { 
       CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); 
       int fabBottomMargin = lp.bottomMargin; 
       int distanceToScroll = fab.getHeight() + fabBottomMargin; 
       float ratio = (float)dependency.getY()/(float)toolbarHeight; 
       fab.setTranslationY(-distanceToScroll * ratio); 
     } 
     return true; 
    } 
}