SoFunction
Updated on 2025-04-09

ListView slides to hide the instance of display ToolBar

introduction

In an era when apps are increasingly pursuing experience, excellent user experience often makes the product stand out. Today we will introduce a simple sliding ListView to display or hide ToolBar.

Layout file

Let’s take a look at the layout file of this main interface. In this layout file, it is mainly a ListView control and a ToolBar control. The layout is as follows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:andro
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <ListView
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#f2f2f2"
  android:divider="#abcdee"
  android:dividerHeight="1px"
  android:>
  
 </ListView>
 <!--ToolBar-->
 <.
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#4097e6"
  android:>
  
 </.>
 
</RelativeLayout>

Main interface code

Implementation ideas:

Let a layout be displayed or hidden and has an animation effect, which we can achieve through attribute animation. The key to achieving this effect is to listen for various sliding events of ListView. We must use the View's OnTouchListener interface to listen for various states. Note:

Since we have added a ToolBar, we need to add a HeadView for the ListView to prevent the ToolBar from blocking the first Item of the ListView.

Let's see the code implementation below:

package .android_view_research;
import ;
import ;
import ;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
public class MainActivity extends Activity {
 private ListView listView;
 String[] datas = {"A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10",
   "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20"};
 private float scaledTouchSlop;
 private float firstY = 0;
 private Toolbar toolbar;
 private ObjectAnimator animtor;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView(.activity_main);
  toolbar = (Toolbar) findViewById();
  listView = (ListView) findViewById();
  /**
    * Add a HeadView to avoid the first Item being blocked by ToolBar
    * Must be set before setAdapter
    */
  initHeadView();
  (new ArrayAdapter&lt;&gt;(this, .simple_list_item_1, datas));
  //Judge the minimum distance to be considered to be sliding (multiple by coefficient to adjust sliding sensitivity)  scaledTouchSlop = (this).getScaledTouchSlop()*3.0f;
  /**
    * Set touch events
    */
  (new () {
   private float currentY;
   private int direction=-1;
   private boolean mShow = true;
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    switch (()) {
     case MotionEvent.ACTION_DOWN:
      firstY = ();
      break;
     case MotionEvent.ACTION_MOVE:
      currentY = ();
      //Swipe down      if (currentY - firstY &gt; scaledTouchSlop) {
       direction = 0;
      }
      //Swipe up      else if (firstY - currentY &gt; scaledTouchSlop) {
       direction = 1;
      }
      //If it is sliding upward and ToolBar is displayed, hide ToolBar      if (direction == 1) {
       if (mShow) {
        toobarAnim(1);
        mShow = !mShow;
       }
      } else if (direction == 0) {
       if (!mShow) {
        toobarAnim(0);
        mShow = !mShow;
       }
      }
      break;
     case MotionEvent.ACTION_UP:
      break;
    }
    return false;//Note that true cannot be returned here, because if true is returned, onTouchEvent cannot be executed, the consequence is that ListView cannot slide   }
  });
 }

 /**
   * Set the head layout, note: the height of this head layout must be consistent with the height of ToolBar
   */
 public void initHeadView() {
  View view = new View(this);
  //abc_action_bar_default_height_material gets the height of the system ActionBar   params = new 
    (.MATCH_PARENT,
      (int) getResources().getDimension(.abc_action_bar_default_height_material));
  (params);
  (view);
 }
 /**
   * ToolBar displays hidden animation
   * @param direction
   */
 public void toobarAnim(int direction) {
  //Cancel the previous animation before starting a new animation  if (animtor != null &amp;&amp; ()) {
   ();
  }
  //() gets the distance from the top of the Toolbar  float translationY=();
  if (direction == 0) {
   animtor = (toolbar, "translationY", translationY, 0);
  } else if (direction == 1) {
   animtor = (toolbar, "translationY", translationY, -());
  }
  ();
 }
}

I believe the comments in the code have been explained in detail. The only thing to note is that the scaledTouchSlop value obtains the minimum sliding distance that the Android system can recognize. By multiplying by the correlation coefficient, we can adjust the sensitivity of the sliding appropriately.

The above example of ListView sliding and hiding display ToolBar is all the content I share with you. I hope you can give you a reference and I hope you can support me more.