溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

listview中上滑下滑監(jiān)聽,上下滑監(jiān)聽隱藏頂部選項欄的示例分析

發(fā)布時間:2021-07-23 11:02:23 來源:億速云 閱讀:169 作者:小新 欄目:移動開發(fā)

小編給大家分享一下listview中上滑下滑監(jiān)聽,上下滑監(jiān)聽隱藏頂部選項欄的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

listview的上滑下滑監(jiān)聽,來隱藏和顯示頂部選項欄的特效,京東 同程等APP的資源列表都有此特效.

兩個重點:

①listview的setOnTouchListener監(jiān)聽方法

當滑動的Y位置減去按下的Y位置大于最小滑動距離時則為向下滑動

反之,當按下的Y位置減去滑動的Y位置大于最小滑動距離則為向上滑動

②位移動畫

就只要這兩點需要注意的,直接上代碼,注釋很清楚。

package com.example.android_topbar_gone;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewConfiguration;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
  private RelativeLayout top_rl;
  private ListView listview;
  private List<Map<String, Object>>list = new ArrayList<Map<String,Object>>();
  private int mTouchShop;//最小滑動距離
  protected float mFirstY;//觸摸下去的位置
  protected float mCurrentY;//滑動時Y的位置
  protected int direction;//判斷是否上滑或者下滑的標志
  protected boolean mShow;//判斷是否執(zhí)行了上滑動畫
  private Animator mAnimator;//動畫屬性
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //初始化id
    setViews();
    //加載listview
    setListView();
  }
  /**
   * 初始化id
   */
  private void setViews() {
    top_rl = (RelativeLayout) findViewById(R.id.rl_ttt);
    listview = (ListView) findViewById(R.id.listview);
  }
  /**
   * 加載listview
   */
  private void setListView() {
    View header = View.inflate(this, R.layout.headview, null);//自定義一個頭布局和頂部執(zhí)行動畫的布局等高就行
    listview.addHeaderView(header);//加載頭布局
    //獲得一個最小滑動距離
    mTouchShop = ViewConfiguration.get(this).getScaledTouchSlop();//系統(tǒng)級別的一個屬性,判斷用戶的最小滑動距離的,可查看源碼為16
    //給集合添加數據
    for (int i = 0; i < 40; i++) {
      Map<String, Object>map = new HashMap<String, Object>();
      map.put("str", "第"+i+"個item");
      list.add(map);
    }
    String a[] = {"str"};
    int b[] = {R.id.tv01};
    //simpleadapter加載集合數據
    SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item, a, b);
    listview.setAdapter(adapter);
    listview.setOnItemClickListener(new OnItemClickListener() {//listview的點擊方法
      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
          long arg3) {
        Toast.makeText(MainActivity.this, list.get(arg2-1).get("str")+"", 0).show();//-1是因為加載的頭布局
      }
    });
    listview.setOnTouchListener(new OnTouchListener() {//listview的觸摸事件
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
          mFirstY = event.getY();//按下時獲取位置
          break;
        case MotionEvent.ACTION_MOVE:
          mCurrentY = event.getY();//得到滑動的位置
          if(mCurrentY - mFirstY > mTouchShop){//滑動的位置減去按下的位置大于最小滑動距離 則表示向下滑動
            direction = 0;//down
          }else if(mFirstY - mCurrentY > mTouchShop){//反之向上滑動
            direction = 1;//up
          }
          if(direction == 1){//判斷如果是向上滑動 則執(zhí)行向上滑動的動畫
            if(mShow){//判斷動畫是否執(zhí)行了 執(zhí)行了則改變狀態(tài)
              //執(zhí)行往上滑動的動畫
              tolbarAnim(1);
              mShow = !mShow;
            }
          }else if(direction == 0){//判斷如果是向下滑動 則執(zhí)行向下滑動的動畫
            if(!mShow){//判斷動畫是否執(zhí)行了 執(zhí)行了則改變狀態(tài)
              //執(zhí)行往下滑動的動畫
              tolbarAnim(0);
              mShow = !mShow;
            }
          }
          break;
        case MotionEvent.ACTION_UP:
          break;
        }
        return false;
      }
    });
  }

  private void tolbarAnim(int flag){
    if(mAnimator != null && mAnimator.isRunning()){//判斷動畫存在 如果啟動了,則先關閉
      mAnimator.cancel();
    }
    if(flag == 0){
      mAnimator = ObjectAnimator.ofFloat(top_rl, "translationY", top_rl.getTranslationY(),0);//從當前位置位移到0位置
    }else{
      mAnimator = ObjectAnimator.ofFloat(top_rl, "translationY", top_rl.getTranslationY(),-top_rl.getHeight());//從當前位置移動到布局負高度的wiz
    }
    mAnimator.start();//執(zhí)行動畫
  }

}
<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" >
  <ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@null"
    android:divider="@null"
    android:listSelector="@android:color/transparent"
    android:scrollbars="@null" >
  </ListView>

  <RelativeLayout 
    android:id="@+id/rl_ttt"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#39caab"
    ></RelativeLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="50dp" >
  </RelativeLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#eeeeee" >
    <TextView
      android:id="@+id/tv01"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerVertical="true"
      android:layout_marginLeft="18dp"
      android:text="第一個item"
      android:textColor="#646464"
      android:textSize="14dp" />
    <TextView
      android:layout_width="match_parent"
      android:layout_height="0.5dp"
      android:layout_alignParentBottom="true"
      android:background="#d2d2d2" />
  </RelativeLayout>
</RelativeLayout>

一個listview的滑動監(jiān)聽動畫實現(xiàn)搞定 很好理解對吧。

看完了這篇文章,相信你對“l(fā)istview中上滑下滑監(jiān)聽,上下滑監(jiān)聽隱藏頂部選項欄的示例分析”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI