Use the View's OnTouchListener interface to monitor the sliding of the listView. By comparing the size of the coordinates with the last time, we judge the sliding direction, and judge whether the corresponding layout needs to be displayed or hidden through the sliding direction, and it has an animation effect.
1. Automatically display hidden Toolbar
First add a HeaderView to the listView to avoid the first Item being blocked by Toolbar.
View header=new View(this); (new ( .MATCH_PARENT, (int)getResources().getDimension(.abc_action_bar_default_height_material))); (header); //.abc_action_bar_default_height_materialFor the systemActionBarThe height of
Define an mTouchSlop variable to obtain the minimum sliding distance considered by the system
mTouchSlop=(this).getScaledTouchSlop();//The minimum sliding distance considered by the system
Determine sliding events
(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (()) { case MotionEvent.ACTION_DOWN: mFirstY=(); break; case MotionEvent.ACTION_MOVE: mCurrentY=(); if(mCurrentY-mFirstY>mTouchSlop) direction=0; //listView slides downelse if(mFirstY-mCurrentY>mTouchSlop) direction=1; //listView slides upif(direction==1) { if(mShow) { toolbarAnim(1); //Hide the view abovemShow=!mShow; } } else if(direction==0) { if(!mShow) { toolbarAnim(0); //Show the view abovemShow=!mShow; } } case MotionEvent.ACTION_UP: break; } return false; } }); }
Attribute animation
protected void toolbarAnim(int flag) { if(set!=null && ()) { (); } if(flag==0) { mAnimator1=(mToolbar, "translationY", (),0); mAnimator2=(mToolbar, "alpha", 0f,1f); } else if(flag==1) { mAnimator1=(mToolbar, "translationY", (),-()); mAnimator2=(mToolbar, "alpha", 1f,0f); } set=new AnimatorSet(); (mAnimator1,mAnimator2); (); } //The above is the displacement and transparency attribute animation
When using the theme, use NoActionBar, otherwise it will cause conflicts. Introduce compilation at the same time
dependencies{ compile fileTree(include:['*.jar'],dir:'libs') compile ':appcompat-v7:21.0.3' }
2. When the component to be hidden and displayed is not the toolbar, but our custom layout of myView, you need to pay attention to some points.
(1) The layout should be used with relative layout, so that our customized layout can be suspended above the listView.
(2) Avoid the first Item being blocked by myView and add a HeaderView to the listView. At this time, you need to measure the height of myView. You must use the following method to post the task to the UI thread, otherwise an error will occur.
final View header=new View(this); //Add a headView to the listView to prevent the first item from being blocked (new Runnable() {public void run() { (new ( .MATCH_PARENT, ())); } });
The others are the same as the toolbar
The above is the implementation method of Android ListView automatic display and hidden layout introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!