Android CoordinatorLayout : ToolBar scroll only when list starts scrolling -
i using toolbar in application , using link hiding tool bar,it works perfect expected.but when list has 1 /two items, there no need scroll tool bar there enough space @ bottom.
the idea behind hiding tool bar make use of tool bar space when list items beyond screen height.but when list items few i.e less device screen height not want scroll tool bar.how achieve it. tia.
code :
<?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:fitssystemwindows="true"> <android.support.v7.widget.recyclerview android:id="@+id/my_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <android.support.design.widget.appbarlayout android:layout_width="match_parent" android:layout_height="wrap_content"> <include android:id="@+id/my_awesome_toolbar" layout="@layout/tool_bar" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" app:layout_scrollflags="scroll|enteralways" /> </android.support.design.widget.appbarlayout> <textview android:id="@+id/empty_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/no_matches_found" android:visibility="gone" /> </android.support.design.widget.coordinatorlayout>
finally logic worked me.
1.get height of recycleview list item , multiply total item count.
- get height of display.
based on above 2 parameters set scroll flags tool bar,like below code :
private void setscrollifneeded() { int viewitemheight = getresources().getdimensionpixelsize(r.dimen.dp); // list item size fixed in case int totalcount = mrecycleview.getadapter().getitemcount(); displaymetrics displaymetrics = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(displaymetrics); int height = displaymetrics.heightpixels; int width = displaymetrics.widthpixels; int listheight = viewitemheight * totalcount; if (listheight > height) { setscrollflags(true); } else { setscrollflags(false); } } public void setscrollflags(boolean set) { appbarlayout.layoutparams params = (appbarlayout.layoutparams) mtooolbar.getlayoutparams(); if (set) params.setscrollflags(appbarlayout.layoutparams.scroll_flag_scroll | appbarlayout.layoutparams.scroll_flag_enter_always); else params.setscrollflags(0); }
Comments
Post a Comment