Public abstract class Recyclerviewscrolllistener extends Recyclerview.onscrolllistener {private static final int SCR Oll_distance = 50; private int totalscrolldistance; Private Boolean isshow = true; @Override public void onscrollstatechanged (Recyclerview recyclerview, int newstate) {super.onscrollstatechanged ( Recyclerview, newstate); } @Override public void onscrolled (recyclerview recyclerview, int dx, int dy) {super.onscrolled (recyclerview, DX, dy); T Firstvisableitem = ((Linearlayoutmanager) Recyclerview.getlayoutmanager ()). Findfirstvisibleitemposition (); Hidden, displayed action if (firstvisableitem==0) {return) is not triggered when the first item has an interface; } if ((Dy > 0 && isshow) | | (Dy < 0 &&!isshow)) {totalscrolldistance + = dy;} if (Totalscrolldistance > Scroll_distance && isshow) {hide (); Isshow = false; totalscrolldistance = 0; } else if (Totalscrolldistance <-scroll_distance &&!isshow) {show (); isshow = true; Totalscrolldistance = 0; }} public abstract void hide (); public abstract void Show ();
Hide and show event handling
mRecyclerView.addOnScrollListener(new RecyclerViewScrollListener() { @Override public void hide() { mToolbar.animate().translationY(-mToolbar.getHeight()).setInterpolator(new AccelerateDecelerateInterpolator()); } @Override public void show() { mToolbar.animate().translationY(0).setInterpolator(new AccelerateDecelerateInterpolator());} });
Effect:
XML layout:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http/ Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context =". Mainactivity "><android.support.v7.widget.recyclerview android:id=" @+id/recyclerview "android:layout_width= "Match_parent" android:paddingtop= "? Attr/actionbarsize" android:cliptopadding= "false" android:layout_height= " Match_parent "/> <android.support.v7.widget.toolbar android:id=" @+id/toolbar "android:layout_width=" Match_ Parent "android:background=" #8803A9F4 "android:layout_height="? actionbarsize "/> </relativelayout>
One of the above Recyclerview useful to a property: android:cliptopadding, this attribute is not much, here is the usage, this property meaning when we set the Paddingtop and other properties, Used to set whether the contents of the list can be drawn in the padding area, which by default is true, indicates that it is not possible to draw in the padding area, false indicates that it can be drawn in the padding area, as shown above, the list area would have been full screen, And I set the height of the paddingtop to Actionbar, so the contents of the list will be under the Actionbar, when sliding up because I set the android:cliptopadding= "false", so can be drawn in the padding area, Below if I set it to true,android:cliptopadding= "true", not aware of the calendar, there must be a blank area above the padding, to see the effect:
Android dropdown slide Show and hide toolbar another implementation