ListView滑動(dòng)隱藏顯示ToolBar的實(shí)例
引言
在App日益追求體驗(yàn)的時(shí)代,優(yōu)秀的用戶體驗(yàn)往往會(huì)使產(chǎn)品脫穎而出。今天我們就來(lái)介紹一種簡(jiǎn)單的滑動(dòng)ListView來(lái)顯示或者隱藏ToolBar的功能。
布局文件
下面我們來(lái)看一下這個(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)畫(huà)效果,我們可以通過(guò)屬性動(dòng)畫(huà)來(lái)實(shí)現(xiàn)。實(shí)現(xiàn)這個(gè)效果的關(guān)鍵就是監(jiān)聽(tīng)ListView的各種滑動(dòng)事件,我們肯定需要借助View的OnTouchListener接口來(lái)監(jiān)聽(tīng)各種狀態(tài)。注意點(diǎn):
由于增加了一個(gè)ToolBar,我們需要為L(zhǎng)istView添加一個(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就無(wú)法執(zhí)行,導(dǎo)致的后果是ListView無(wú)法滑動(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)畫(huà) * @param direction */ public void toobarAnim(int direction) { //開(kāi)始新的動(dòng)畫(huà)之前要先取消以前的動(dòng)畫(huà) 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ò)乘以相關(guān)系數(shù),可以適當(dāng)?shù)恼{(diào)整滑動(dòng)的靈敏度。
以上這篇ListView滑動(dòng)隱藏顯示ToolBar的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android 實(shí)現(xiàn)圖片轉(zhuǎn)二進(jìn)制流及二進(jìn)制轉(zhuǎn)字符串
這篇文章主要介紹了Android 實(shí)現(xiàn)圖片轉(zhuǎn)二進(jìn)制流及二進(jìn)制轉(zhuǎn)字符串,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03android端微信支付V3版本地簽名統(tǒng)一下單詳解
本篇文章主要介紹了android端微信支付V3版本地簽名統(tǒng)一下單,具有一定的參考價(jià)值,有興趣的同學(xué)可以了解一下。2016-11-11ListView實(shí)現(xiàn)下拉刷新加載更多的實(shí)例代碼(直接拿來(lái)用)
這篇文章主要介紹了ListView實(shí)現(xiàn)下拉刷新加載更多的實(shí)例代碼(直接拿來(lái)用)的相關(guān)資料,需要的朋友可以參考下2016-07-07Android 對(duì)話框(Dialog)大全示例(建立你自己的對(duì)話框)
android開(kāi)發(fā)中,對(duì)話框的使用還是很平凡的,本篇文章介紹了Android 對(duì)話框的實(shí)例,詳細(xì)的介紹了多種對(duì)話框的方法,有興趣的可以了解一下。2016-11-11Android使用ViewPager實(shí)現(xiàn)無(wú)限滑動(dòng)效果
相信在大家開(kāi)發(fā)Android的時(shí)候,我們常常用ViewPager來(lái)為自己的應(yīng)用創(chuàng)建廣告條幅,并且常常會(huì)遇到ViewPager無(wú)限滑動(dòng)這樣的需求。下面來(lái)一起看看吧。2016-09-09Android輕松實(shí)現(xiàn)多語(yǔ)言的方法示例
本篇文章主要介紹了Android輕松實(shí)現(xiàn)多語(yǔ)言的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Android 仿微信發(fā)動(dòng)態(tài)九宮格拖拽、刪除功能
這篇文章主要介紹了Android 仿微信發(fā)動(dòng)態(tài)九宮格拖拽、刪除功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11