Android dropdown slide Show and hide toolbar another implementation

Source: Internet
Author: User

About the introduction is not said, do not use nested sliding case, another implementation, of course, you can also use the design package under the Coordinatorlayout.behavior. More straightforward to add code directly:

Inheriting Recyclerview.onscrolllistener overrides the Onscrolled method, while defining two abstract methods for us to handle hiding and displaying events
 Public Abstract  class recyclerviewscrolllistener extends recyclerview. Onscrolllistener {    Private Static Final intScroll_distance = -;Private intTotalscrolldistance;Private BooleanIsshow =true;@Override     Public void onscrollstatechanged(Recyclerview Recyclerview,intNewState) {Super. onscrollstatechanged (Recyclerview, newstate); }@Override     Public void onscrolled(Recyclerview Recyclerview,intDxintDY) {Super. onscrolled (Recyclerview, DX, dy);intFirstvisableitem = ((Linearlayoutmanager) Recyclerview.getlayoutmanager ()). Findfirstvisibleitemposition ();//When the first item has an interface, no hidden, display actions are triggered        if(firstvisableitem==0){return; }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            publicvoidhide() {                mToolbar.animate().translationY(-mToolbar.getHeight()).setInterpolator(new AccelerateDecelerateInterpolator());            }            @Override            publicvoidshow() {                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  = android:cliptopadding  =" false " android:layout_height  = "match_parent" />     <android.support.v7.widget.Toolbarandroid: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 property is not used much, here to illustrate the usage, this property meaning when we set the Paddingtop and other properties, to set the contents of the list can be drawn in the padding area, The default is true, which means that you cannot draw in the padding area, false indicates that you can draw in the padding area, as shown above, the list area would have been full screen, and I set the height of paddingtop to Actionbar, So the contents of the list will be under the Actionbar, when the upward sliding because I set android:clipToPadding="false" , so can be drawn in the padding area, below I if I set it to true, not aware of the android:clipToPadding="true" calendar, it must be above a padding blank area, to see the effect:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Reprint Annotated Source: http://blog.csdn.net/u010687392

Android dropdown slide Show and hide toolbar another implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.