Android-Toolbar Follow the ListView slide hide and show

Source: Internet
Author: User

Layout
<Relativelayoutxmlns: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">      <ListViewAndroid:id= "@+id/main_list_view"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"          />      <Android.support.v7.widget.ToolbarAndroid:id= "@+id/main_bar"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:background= "@color/std_color_a"          />  </Relativelayout>
Code
 Public classMainactivityextendsbaseactivity{Private Static FinalString TAG = mainactivity.class. Getsimplename (); PrivateToolbar Mmaintoolbar =NULL; PrivateListView Mmainlistview =NULL; Private floatMstarty = 0, mlasty = 0, Mlastdeltay; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);            Setcontentview (R.layout.activity_main); Mmaintoolbar= (Toolbar) This. Findviewbyid (R.id.main_bar);  This. Setsupportactionbar (Mmaintoolbar); Mmainlistview= (ListView) This. Findviewbyid (R.id.main_list_view); FinalView Header = Layoutinflater.from ( This). Inflate (R.layout.layout_header,NULL);          //This header should be the same height as the toolbar Mmainlistview.addheaderview (header); Mmainlistview.setadapter (NewAudioadapter ( This)); Mmainlistview.setontouchlistener (NewView.ontouchlistener () {@Override Public BooleanOnTouch (View V, motionevent event) {Final floaty =event.gety (); floatTranslationy =Mmaintoolbar.gettranslationy (); Switch(Event.getaction ()) { CaseMotionevent.action_down:Mstarty =y; Mlasty=Mstarty;  Break;  CaseMotionevent.action_move:floatMdeltay = y-Mlasty; floatNewtansy = Translationy +Mdeltay; if(newtansy <= 0 && Newtansy >=-mmaintoolbar.getheight ())                          {mmaintoolbar.settranslationy (Newtansy); } mlasty=y; Mlastdeltay=Mdeltay;                          Break;  CaseMotionEvent.ACTION_UP:ObjectAnimator Animator=NULL; LOG.D (TAG,"Mlastdeltay=" +Mlastdeltay); if(Mlastdeltay < 0 && mmainlistview.getfirstvisibleposition () > 1) {log.v (TAG,"Listview.first=" +mmainlistview.getfirstvisibleposition ()); Animator= Objectanimator.offloat (Mmaintoolbar, "Translationy", Mmaintoolbar.gettranslationy (),-mmaintoolbar.getheight ()); } Else{Animator= Objectanimator.offloat (Mmaintoolbar, "Translationy", Mmaintoolbar.gettranslationy (), 0); } animator.setduration (100);                          Animator.start (); Animator.setinterpolator (Animationutils.loadinterpolator (mainactivity. This, Android.  R.interpolator.linear));                          Break; }                  return false;      }          }); }  }

First with our own toolbar replace the original Actionbar, notice, in your apptheme,windowactionbar This item to set to False in order to use our own to replace the original, or run will error , Then give the ListView a high Headerview such as toolbar. Then we set the touch event listener, in the Action_move branch of the Ontouch method, we calculated the change in the position of our trigger point-mdeltay when the move event was triggered with the last move or down event. And then calculates a corresponding translationy, after comparing with the toolbar height, judging whether the new translationy is lawful and lawful, then using the Settranslationy method, assigns the value to the toolbar. Trigger up event: When the up event is triggered, we need to use an animation to overdo it. First, the direction of the slide, the direction upward, then the upward slide, downward, then the downward movement.

I'm the dividing line of the king of the Land Tiger.

Reference: http://blog.csdn.net/boybeak/article/details/41410113

Android-Toolbar Follow the ListView slide hide and show

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.