Android Toolbar Follow the ListView sliding hidden and reality

Source: Internet
Author: User

Anyone who has used the Google Play store app or the app is aware that its actionbar can be hidden or displayed as the ListView slides. The effect looks very good, for this, I clumsily imitated a similar effect, do not know there is no better way.

First on the main layout activity_main:

<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= "com.beak.music.ui.MainActivity" >    <listview        android:id= "@+id/main_list_view"        android:layout_width= "fill_parent"        android:layout_height= "fill_parent"        />    < Android.support.v7.widget.Toolbar        android:id= "@+id/main_bar"        android:layout_width= "Fill_parent"        android:layout_height= "wrap_content"        android:background= "@color/std_color_a"        /></ Relativelayout>
Note that the toolbar here, we will use this toolbar to replace the original Actionbar.

Now is the code in Mainactivity.java:

public class Mainactivity extends baseactivity{private static final String TAG = MainActivity.class.getSimpleName ();    Private Toolbar Mmaintoolbar = null;    Private ListView Mmainlistview = null;    private float Mstarty = 0, mlasty = 0, Mlastdeltay;        @Override protected void OnCreate (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);        Final View Header = Layoutinflater.from (this). Inflate (R.layout.layout_header, NULL);        Mmainlistview.addheaderview (header);        Mmainlistview.setadapter (New Audioadapter (this)); Mmainlistview.setontouchlistener (New View.ontouchlistener () {@Override public boolean OnTouch (View        V, motionevent event) {final float y = event.gety ();        float translationy = Mmaintoolbar.gettranslationy ();                         Switch (event.getaction ()) {case motionevent.action_down://log.v (TAG, ' down ');                        Mstarty = y;                        Mlasty = Mstarty;                    Break                        Case MotionEvent.ACTION_MOVE:float Mdeltay = y-mlasty;                        Float Newtansy = translationy + Mdeltay; if (newtansy <= 0 && newtansy >=-mmaintoolbar.getheight ()) {Mmaintoolbar.settra                        Nslationy (Newtansy);                        } mlasty = y;                        Mlastdeltay = mdeltay;//log.v (TAG, "Move");                    Break                        Case MotionEvent.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.gettransl                        Ationy (), 0);                        } animator.setduration (100);                        Animator.start (); Animator.setinterpolator (Animationutils.loadinterpolator (Mainactivity.this, Android.                        r.interpolator.linear));//LOG.V (TAG, "up");                Break            } return false;    }        }); }}
The main problem is the ListView swipe gesture detection and animation in toolbar.

First, with our own toolbar replace the original Actionbar, note, in your apptheme, Windowactionbar this item to be 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. And then set up the touch event listener,

In the Action_move branch of the Ontouch method, we calculate the change in the position of our trigger point-mdeltay when the move event is triggered with the last move or down event, and then calculate a corresponding Translationy, After comparing with the toolbar height, judging whether the new translationy is lawful and lawful, the Settranslationy method is used to assign the value to toolbar.

To trigger the 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.

Android Studio Project code codes here, but Android Studio code, no ADT code written



Android Toolbar Follow the ListView sliding hidden and reality

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.