ListView滑動(dòng)隱藏顯示ToolBar的實(shí)例
引言
在App日益追求體驗(yàn)的時(shí)代,優(yōu)秀的用戶體驗(yàn)往往會(huì)使產(chǎn)品脫穎而出。今天我們就來介紹一種簡單的滑動(dòng)ListView來顯示或者隱藏ToolBar的功能。
布局文件
下面我們來看一下這個(gè)主界面的布局文件。在這個(gè)布局文件中,主要是一個(gè)ListView控件和一個(gè)ToolBar控件。布局如下:
<?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"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f2f2f2" android:divider="#abcdee" android:dividerHeight="1px" android:id="@+id/listView"> </ListView> <!--ToolBar--> <android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#4097e6" android:id="@+id/toolBar"> </android.support.v7.widget.Toolbar> </RelativeLayout>
主界面代碼
實(shí)現(xiàn)思路:
讓一個(gè)布局顯示或者隱藏并且?guī)в袆?dòng)畫效果,我們可以通過屬性動(dòng)畫來實(shí)現(xiàn)。實(shí)現(xiàn)這個(gè)效果的關(guān)鍵就是監(jiān)聽ListView的各種滑動(dòng)事件,我們肯定需要借助View的OnTouchListener接口來監(jiān)聽各種狀態(tài)。注意點(diǎn):
由于增加了一個(gè)ToolBar,我們需要為ListView添加一個(gè)HeadView,防止ToolBar擋住ListView的第一個(gè)Item。
下面看代碼實(shí)現(xiàn):
package com.research.gong.android_view_research;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolBar);
listView = (ListView) findViewById(R.id.listView);
/**
* 添加一個(gè)HeadView避免第一個(gè)Item被ToolBar遮擋
* 必須在setAdapter之前進(jìn)行設(shè)置
*/
initHeadView();
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, datas));
//判斷認(rèn)為是滑動(dòng)的最小距離(乘以系數(shù)調(diào)整滑動(dòng)靈敏度)
scaledTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop()*3.0f;
/**
* 設(shè)置觸摸事件
*/
listView.setOnTouchListener(new View.OnTouchListener() {
private float currentY;
private int direction=-1;
private boolean mShow = true;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
firstY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
currentY = event.getY();
//向下滑動(dòng)
if (currentY - firstY > scaledTouchSlop) {
direction = 0;
}
//向上滑動(dòng)
else if (firstY - currentY > scaledTouchSlop) {
direction = 1;
}
//如果是向上滑動(dòng),并且ToolBar是顯示的,就隱藏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;//注意此處不能返回true,因?yàn)槿绻祷豻rue,onTouchEvent就無法執(zhí)行,導(dǎo)致的后果是ListView無法滑動(dòng)
}
});
}
/**
* 設(shè)置頭布局,注意:這個(gè)頭布局的高度要和ToolBar的高度一致
*/
public void initHeadView() {
View view = new View(this);
//abc_action_bar_default_height_material獲取系統(tǒng)ActionBar的高度
AbsListView.LayoutParams params = new AbsListView.LayoutParams
(AbsListView.LayoutParams.MATCH_PARENT,
(int) getResources().getDimension(R.dimen.abc_action_bar_default_height_material));
view.setLayoutParams(params);
listView.addHeaderView(view);
}
/**
* ToolBar顯示隱藏動(dòng)畫
* @param direction
*/
public void toobarAnim(int direction) {
//開始新的動(dòng)畫之前要先取消以前的動(dòng)畫
if (animtor != null && animtor.isRunning()) {
animtor.cancel();
}
//toolbar.getTranslationY()獲取的是Toolbar距離自己頂部的距離
float translationY=toolbar.getTranslationY();
if (direction == 0) {
animtor = ObjectAnimator.ofFloat(toolbar, "translationY", translationY, 0);
} else if (direction == 1) {
animtor = ObjectAnimator.ofFloat(toolbar, "translationY", translationY, -toolbar.getHeight());
}
animtor.start();
}
}
相信代碼中注釋已經(jīng)解釋的很詳細(xì)了。唯一需要注意的是:scaledTouchSlop值默認(rèn)獲取的是Android系統(tǒng)能識(shí)別的最小滑動(dòng)距離。我們通過乘以相關(guān)系數(shù),可以適當(dāng)?shù)恼{(diào)整滑動(dòng)的靈敏度。
以上這篇ListView滑動(dòng)隱藏顯示ToolBar的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android 基礎(chǔ)入門教程——開發(fā)環(huán)境搭建
這篇文章主要介紹了Android 如何搭建開發(fā)環(huán)境,文中講解非常細(xì)致,幫助大家開始學(xué)習(xí)Android,想要學(xué)習(xí)Android的朋友可以了解下2020-06-06
Android基礎(chǔ)控件RadioGroup使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android基礎(chǔ)控件RadioGroup的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android?Material組件庫日期選擇和時(shí)間選擇器的使用方法
這篇文章主要介紹了Android?Material組件庫(日期選擇和時(shí)間選擇器)基本使用,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11
Android Zipalign工具優(yōu)化Android APK應(yīng)用
本文主要介紹Android Zipalign工具優(yōu)化Android APK應(yīng)用,這里整理了相關(guān)資料及簡單優(yōu)化實(shí)例,有需要的小伙伴可以參考下2016-09-09
Input系統(tǒng)之InputReader處理按鍵事件詳解
這篇文章主要為大家介紹了Input系統(tǒng)之InputReader處理按鍵事件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

