欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ListView上滑和下滑,顯示和隱藏Toolbar的實現(xiàn)方法

 更新時間:2018年01月04日 10:35:50   作者:YunYIS  
下面小編就為大家分享一篇ListView上滑和下滑,顯示和隱藏Toolbar的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

1.準備Toolbar

先隱藏系統(tǒng)自帶的actionbar,在AndroidManifest.xml文件<application>標簽中:

android:theme="@style/Theme.AppCompat.Light.NoActionBar">

(注意此處的Activity應(yīng)繼承AppCompatActivity)

再在布局文件中添加Toolbar,activity_main.xml文件中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.listview_unit4_1.MainActivity">
 <ListView
  android:id="@+id/list_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>
 <android.support.v7.widget.Toolbar
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="?attr/actionBarSize"
  android:background="@color/colorPrimary"/>
</RelativeLayout>

現(xiàn)在布局中就添加了一個Toolbar和一個ListView.

2.Activity

package com.example.listview_unit4_1;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
 private ListView listView;
 private List<String> listViewData;//數(shù)據(jù)源
 private ArrayAdapter<String> adapter;
 private Toolbar toolbar;
 private int mTouchSlop;//系統(tǒng)認為的最小滑動距離
 private float mFirstY;//用戶首次觸摸的Y坐標
 private float mLastY;//用戶滑動結(jié)束時Y坐標
 private ObjectAnimator mAnimator;//將控件與動畫聯(lián)系起來的類(可以使指定的控件,實現(xiàn)指定的動畫效果)
 private boolean mShow;//toolbar是否顯示
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  toolbar = (Toolbar) findViewById(R.id.toolbar);
  setSupportActionBar(toolbar);
  //初始化數(shù)據(jù)源
  initData();
  listView = (ListView) findViewById(R.id.list_view);
  //為ListView增加一個HeadView(避免第一個Item被toolbar遮擋)
  //abc_action_bar_default_height_material屬性獲取系統(tǒng)actionBar的高度
  View headView = new View(this);
  headView.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT,
    (int)getResources().getDimension(R.dimen.abc_action_bar_default_height_material)));
  listView.addHeaderView(headView);
  //獲取系統(tǒng)定義的最低滑動距離
  mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();
  adapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, listViewData);
  listView.setAdapter(adapter);
  //為ListView設(shè)置觸摸事件監(jiān)聽
  listView.setOnTouchListener(new View.OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()){
     case MotionEvent.ACTION_DOWN:
      mFirstY = event.getY();//getY獲取的是相對于View的坐標,getRawY獲取的是相對于屏幕的坐標
      break;
     case MotionEvent.ACTION_MOVE:
      mLastY = event.getY();
      if(mLastY - mFirstY > mTouchSlop){//手指向下滑動,顯示toolbar
       if(!mShow){
        Log.i("tag", "mLastY_手指下滑="+mLastY);
        toolbarAnim(0);//顯示
        mShow = !mShow;
       }
      }else if(mFirstY - mLastY > mTouchSlop){//手指向上滑動,隱藏toolbar
       if(mShow){
        Log.i("tag", "mLastY_手指上滑="+mLastY);
        toolbarAnim(1);//隱藏
        mShow = !mShow;
       }
      }
      break;
     default:break;
    }
    return false;//一般返回false,提交給上級
   }
  });
 }
 public void toolbarAnim(int flag){
  if(mAnimator != null && mAnimator.isRunning()){
   mAnimator.cancel();
  }
  if(flag == 0){
   Log.i("tag", "手指下滑,》》》》》》》顯示");
   mAnimator = ObjectAnimator.ofFloat(toolbar, "translationY", toolbar.getTranslationY(), 0);
  }else if(flag == 1){
   Log.i("tag", "手指上滑,》》》》》》》隱藏");
   mAnimator = ObjectAnimator.ofFloat(toolbar, "translationY", toolbar.getTranslationY(),
     -toolbar.getHeight());
  }
  mAnimator.start();//開始動畫
 }
 /**
  * 初始化ListView的數(shù)據(jù)源
  */
 public void initData(){
  listViewData = new ArrayList<>();
  String s;
  for(int i = 0; i < 20; i ++){
   s = ""+i;
   listViewData.add(s);
  }
 }
}

(1)為ListView添加了一個HeadView高度與Toolbar高度一致,避免在 RelativeLayout中Toolbar遮擋ListView第一項;

(2)設(shè)置ListView的觸摸監(jiān)聽事件:

當用戶首次按下(MotionEvent.ACTION_DOWN),記錄開始的Y坐標;在用戶滑動的過程中(MotionEvent.ACTION_MOVE),記錄用戶當前的手指的Y坐標;兩者比較可以判斷出用戶手指滑動方向;

(3)使用 ObjectAnimator類控制Toolbar的動畫效果:

實例化mAnimator:

//第一個參數(shù)用于指定這個動畫要操作的是哪個控件
//第二個參數(shù)用于指定這個動畫要操作這個控件的哪個屬性
//第三個參數(shù)是可變長參數(shù),指這個屬性值是從多少變到多少
mAnimator = ObjectAnimator.ofFloat(toolbar, "translationY", toolbar.getTranslationY(), 0);
mAnimator = ObjectAnimator.ofFloat(toolbar, "translationY", toolbar.getTranslationY(),
     -toolbar.getHeight());

(4)使用一個布爾值標識當前Toolbar是否顯示:

如果不是這樣做,那用戶在不斷滑動過程中,會不斷的觸發(fā)onTouch方法,不斷地執(zhí)行toolbarAnim方法,導(dǎo)致滑動效果很不流暢(從Log中可以發(fā)現(xiàn)這一點)

實現(xiàn)效果:

以上這篇ListView上滑和下滑,顯示和隱藏Toolbar的實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android StickListView實現(xiàn)懸停效果

    Android StickListView實現(xiàn)懸停效果

    這篇文章主要介紹了Android StickListView實現(xiàn)懸停效果的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android中監(jiān)聽軟鍵盤輸入的使用方式

    Android中監(jiān)聽軟鍵盤輸入的使用方式

    今天我們來討論一下Android中監(jiān)聽軟鍵盤輸入的使用方式,它允許用戶輸入文本和執(zhí)行其他操作,但是,有時候我們需要在用戶輸入文本時進行一些特殊的處理,比如實時驗證輸入內(nèi)容、限制輸入字符的類型等,因此,了解如何監(jiān)聽軟鍵盤輸入是非常重要的
    2023-10-10
  • Android 編程下的計時器代碼

    Android 編程下的計時器代碼

    在安卓 APP 的手機號注冊邏輯中,經(jīng)常會有將激活碼發(fā)送到手機的環(huán)節(jié),這個環(huán)節(jié)中絕大多數(shù)的應(yīng)用考慮到網(wǎng)絡(luò)延遲或服務(wù)器壓力以及短信服務(wù)商的延遲等原因,會給用戶提供一個重新獲取激活碼的按鈕
    2013-08-08
  • Android RecyclerView實現(xiàn)吸頂動態(tài)效果流程分析

    Android RecyclerView實現(xiàn)吸頂動態(tài)效果流程分析

    RecyclerView是Android一個更強大的控件,其不僅可以實現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實現(xiàn)數(shù)據(jù)縱向滾動,也可以實現(xiàn)橫向滾動(ListView做不到橫向滾動)。接下來講解RecyclerView的用法
    2022-12-12
  • Android實現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單(2)

    Android實現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單(2)

    這篇文章主要為大家詳細介紹了Android實現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單第二篇,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android組件實現(xiàn)長按彈出上下文菜單功能的方法

    Android組件實現(xiàn)長按彈出上下文菜單功能的方法

    這篇文章主要介紹了Android組件實現(xiàn)長按彈出上下文菜單功能的方法,結(jié)合實例形式分析了Android實現(xiàn)長按彈出上下文菜單的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-07-07
  • 完美解決EditText和ScrollView的滾動沖突(上)

    完美解決EditText和ScrollView的滾動沖突(上)

    這篇文章主要為大家詳細介紹了完美解決EditText和ScrollView滾動沖突的方法,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 詳解Android 檢測權(quán)限的三種寫法

    詳解Android 檢測權(quán)限的三種寫法

    這篇文章主要介紹了詳解Android 檢測權(quán)限的三種寫法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • Android實現(xiàn)調(diào)用手機攝像頭錄像限制錄像時長

    Android實現(xiàn)調(diào)用手機攝像頭錄像限制錄像時長

    這篇文章主要為大家詳細介紹了Android實現(xiàn)調(diào)用手機攝像頭錄像限制錄像時長,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 基于barcodescanner實現(xiàn)Android二維碼掃描功能

    基于barcodescanner實現(xiàn)Android二維碼掃描功能

    這篇文章主要為大家詳細介紹了基于barcodescanner實現(xiàn)Android二維碼掃描功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07

最新評論