Complete engineering code in this: Https://github.com/NashLegend/Auto-Hide-ListView
Many software now have this sliding list to automatically hide the function of the page header and the bottom element, such as Google +. When the activity is just entered, the page is a list, there is a view at the bottom, a view on the head, when the list is sliding up, hide the kinsoku elements to show more content, when the list is sliding down, and then pull out the tail and back elements. Like Google +.
Just enter it this way:
Then put the list on the body of a pull, the tail and hide, as this looks like:
If you pull down again, you'll change back to the first picture.
This example implements this function
In this example, the layout of the mainactivity is as follows, toolbar is the top element and the button is the bottom element.
<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 "> <listview android:id=" @+id/list_view "android:layout_width=" Match_parent "an droid:layout_height= "Match_parent" android:headerdividersenabled= "false"/> <android.support.v7.widget.to Olbar android:id= "@+id/action_bar" android:layout_width= "match_parent" android:layout_height= "? attr/a Ctionbarsize "android:background=" @android: Color/holo_blue_light "/> <button android:id=" @+id/foot Er "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_alignpare Ntbottom= "true" android:text= "@string/scrolldown"/></relativelayout>
public class Mainactivity extends Actionbaractivity {listview listview; Toolbar Toolbar; View header; View footer; int touchslop = 10; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Touchslop = (int) (Viewconfiguration.get (mainactivity.this). Getscaledtouchslop () * 0.9);//How many distances are scrolled before starting to calculate whether to hide/show the kinsoku element. This uses 0.9 times times the default touchslop. ListView = (ListView) Findviewbyid (R.id.list_view); Footer = Findviewbyid (r.id.footer); toolbar = (toolbar) Findviewbyid (R.id.action_bar); The following sentence will set this toolbar to Actionbar, in this example, this sentence is actually unnecessary, but if you use this sentence, you have to set theme to Noactionbar, nothing to say here, specifically see the above link in the style Setsupportactionbar (toolbar); Fills the element for this listview. string[] str = new STRING[64]; for (int i = 0; i < str.length; i++) {Str[i] = "Android" + i; }//r.layout.simple_layout is a textview, see the link above ... Arrayadapter<string> adapter = new Arrayadapter<> (mainactivity.this, r.layout.simple_layout, str); Listview.setadapter (adapter); Add a header to the ListView, which is as high as toolbar. This way we can correctly see the first element in the list without being obscured. Header = new View (mainactivity.this); Header.setlayoutparams (New Abslistview.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, (int) getresources (). Getdimension (r.dimen.abc_action_bar_default_height_material)); Header.setbackgroundcolor (Color.parsecolor ("#00000000")); Listview.addheaderview (header); Set touch events and scrolling events for the ListView, which is the core listview.setontouchlistener (Ontouchlistener); Listview.setonscrolllistener (Onscrolllistener); Footer.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { Set the Click event for the button, click once to scroll 10 item listview.smoothscrollbyoffset (10); } }); } @Override Public Boolean oncreateoptionsMenu (Menu menu) {getmenuinflater (). Inflate (R.menu.menu_main, menu); return true; } @Override public boolean onoptionsitemselected (MenuItem item) {int id = item.getitemid (); if (id = = r.id.action_settings) {return true; } return super.onoptionsitemselected (item); } Animatorset backanimatorset;//This is the animation used to display the kinsoku element private void Animateback () {//clear other animations first if (Hideanimato RSet! = null && hideanimatorset.isrunning ()) {hideanimatorset.cancel (); } if (Backanimatorset! = null && backanimatorset.isrunning ()) {//If this animation is already running, just ignore it} ELS e {backanimatorset = new animatorset (); The next two sentences are to put the kinsoku elements back into their original positions. Objectanimator headeranimator = objectanimator.offloat (toolbar, "Translationy", Toolbar.gettranslationy (), 0f); Objectanimator footeranimator = objectanimator.offloat (footer, "Translationy", Footer.gettranslationy (), 0f); arraylist<animator> animators = new arraylist<> (); Animators.add (Headeranimator); Animators.add (Footeranimator); Backanimatorset.setduration (300); Backanimatorset.playtogether (animators); Backanimatorset.start (); }} Animatorset hideanimatorset;//This is the animation used by the hidden kinsoku element private void Animatehide () {//clear other animations first if (Backan Imatorset! = null && backanimatorset.isrunning ()) {backanimatorset.cancel (); } if (Hideanimatorset! = null && hideanimatorset.isrunning ()) {//If this animation is already running, just ignore it} ELS e {hideanimatorset = new animatorset (); Objectanimator headeranimator = objectanimator.offloat (toolbar, "Translationy", Toolbar.gettranslationy (),- Toolbar.getheight ());//Hide toolbar above objectanimator footeranimator = objectanimator.offloat (Footer, "Translatio NY ", Footer.gettranslationy (), Footer.getheight ());//Will Button hidden below arraylist<animator> animators = new arraylist<> (); Animators.add (Headeranimator); Animators.add (Footeranimator); Hideanimatorset.setduration (200); Hideanimatorset.playtogether (animators); Hideanimatorset.start (); }} View.ontouchlistener Ontouchlistener = new View.ontouchlistener () {float lasty = 0f; float currenty = 0f; The following two indicate the direction of the slide, greater than 0 means sliding down, less than 0 means sliding upward, equal to 0 means not sliding int lastdirection = 0; int currentdirection = 0; @Override public boolean OnTouch (View V, motionevent event) {switch (event.getaction ()) { Case MotionEvent.ACTION_DOWN:lastY = Event.gety (); CurrentY = Event.gety (); currentdirection = 0; lastdirection = 0; Break Case MotionEvent.ACTION_MOVE:if (Listview.getfirstvisibleposition () > 0) {//Only when listview.getfirstvisibleposition () >0 is the time to determine whether or not to animate. Because Listview.getfirstvisibleposition () ==0,//toolbar--is the head element must be visible, if this time hidden, then occupy the position with the Headerview is the user Found//But when the user pulls the list down to the Headerview of the list, it's time to make the tail-end element reappear--the judgment is written in the back Onscrolllistener. float Tmpcurrenty = event.gety (); if (Math.Abs (tmpcurrenty-lasty) > Touchslop) {//sliding distance greater than Touchslop is only judged currenty = Tmpcurren TY; currentdirection = (int) (currenty-lasty); if (lastdirection! = currentdirection) {//If it is different from the previous direction, the explicit/hidden animation is performed if (Currentdirection < 0) {animatehide (); } else {animateback (); } } }} break; Case MotionEvent.ACTION_CANCEL:case MOTIONEVENT.ACTION_UP://When the finger is lifted, the currentdirection is set Set to 0, so the next time no matter where to pull, are different from the current (in fact, after writing in the Action_down here will not need ...) ) currentdirection = 0; lastdirection = 0; Break } return false; } }; Abslistview.onscrolllistener Onscrolllistener = new Abslistview.onscrolllistener () {//This listener is actually used to deal with when the user's hand is away from After the list is still sliding, that is, scroll_state_fling int lastposition = 0;//The position of the first visible element in the ListView that was last scrolled--firstvisibleitem int state = Scroll_state_idle; @Override public void onscrollstatechanged (Abslistview view, int scrollstate) {//record current list status St ate = scrollstate; } @Override public void onscroll (Abslistview view, int firstvisibleitem, int visibleitemcount, int totalite MCount) { if (Firstvisibleitem = = 0) {animateback (); if (Firstvisibleitem > 0) {if (Firstvisibleitem > lastposition && state = = Scro ll_state_fling) {//If the last position is less than the current position, then hide the kinsoku element animatehide (); }//================================ if (Firstvisibleitem < lastposition && state = = scroll_state_fling) {//If the last position is greater than the current position, then display the kinsoku element, in this case, the if is useless//AS The result is sliding the ListView trigger, then, Animateback () must have been executed, so there is no need//If it is click button what trigger scrolling, then according to the design principle, the button is one of the kinsoku, so also do not need animateback ( )//So this if block is not required animateback (); }//There is no judgment (Firstvisibleitem = = Lastposition && state = = scroll_state_fling),//But If a single item in the list is too long to judge, just a few more lines of code//But you can trickery, drag to execute Animatehide () or Animateback () when the swipe is triggered-in this case, it's written in that click.It can be done)//BTW, if the list is sliding purely by hand sliding the list, and not similar to clicking a button to roll to a location, as long as the first if is enough ...} Lastposition = Firstvisibleitem; } };}
Example of sliding ListView Auto Hide page header and bottom element