xml - Hide Floating Action Button in Android unless position in Recycler View is at the very top -
i'm trying display floating action button when recycler view position @ top. there anyway can achieve this?
here current layout file:
<?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>
thanks :]
thanks daniel finding link: https://mzgreen.github.io/2015/06/23/how-to-hideshow-toolbar-when-list-is-scrolling%28part3%29/ solved problem
add fab button coordinator layout:
<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" />
add class project:
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; } }
Comments
Post a Comment