ListView滑動隱藏顯示ToolBar的實例
引言
在App日益追求體驗的時代,優(yōu)秀的用戶體驗往往會使產(chǎn)品脫穎而出。今天我們就來介紹一種簡單的滑動ListView來顯示或者隱藏ToolBar的功能。
布局文件
下面我們來看一下這個主界面的布局文件。在這個布局文件中,主要是一個ListView控件和一個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>
主界面代碼
實現(xiàn)思路:
讓一個布局顯示或者隱藏并且?guī)в袆赢嬓Ч?,我們可以通過屬性動畫來實現(xiàn)。實現(xiàn)這個效果的關鍵就是監(jiān)聽ListView的各種滑動事件,我們肯定需要借助View的OnTouchListener接口來監(jiān)聽各種狀態(tài)。注意點:
由于增加了一個ToolBar,我們需要為ListView添加一個HeadView,防止ToolBar擋住ListView的第一個Item。
下面看代碼實現(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); /** * 添加一個HeadView避免第一個Item被ToolBar遮擋 * 必須在setAdapter之前進行設置 */ initHeadView(); listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, datas)); //判斷認為是滑動的最小距離(乘以系數(shù)調(diào)整滑動靈敏度) scaledTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop()*3.0f; /** * 設置觸摸事件 */ 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(); //向下滑動 if (currentY - firstY > scaledTouchSlop) { direction = 0; } //向上滑動 else if (firstY - currentY > scaledTouchSlop) { direction = 1; } //如果是向上滑動,并且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,因為如果返回true,onTouchEvent就無法執(zhí)行,導致的后果是ListView無法滑動 } }); } /** * 設置頭布局,注意:這個頭布局的高度要和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顯示隱藏動畫 * @param direction */ public void toobarAnim(int direction) { //開始新的動畫之前要先取消以前的動畫 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)解釋的很詳細了。唯一需要注意的是:scaledTouchSlop值默認獲取的是Android系統(tǒng)能識別的最小滑動距離。我們通過乘以相關系數(shù),可以適當?shù)恼{(diào)整滑動的靈敏度。
以上這篇ListView滑動隱藏顯示ToolBar的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Android 實現(xiàn)圖片轉(zhuǎn)二進制流及二進制轉(zhuǎn)字符串
這篇文章主要介紹了Android 實現(xiàn)圖片轉(zhuǎn)二進制流及二進制轉(zhuǎn)字符串,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03android端微信支付V3版本地簽名統(tǒng)一下單詳解
本篇文章主要介紹了android端微信支付V3版本地簽名統(tǒng)一下單,具有一定的參考價值,有興趣的同學可以了解一下。2016-11-11ListView實現(xiàn)下拉刷新加載更多的實例代碼(直接拿來用)
這篇文章主要介紹了ListView實現(xiàn)下拉刷新加載更多的實例代碼(直接拿來用)的相關資料,需要的朋友可以參考下2016-07-07Android 對話框(Dialog)大全示例(建立你自己的對話框)
android開發(fā)中,對話框的使用還是很平凡的,本篇文章介紹了Android 對話框的實例,詳細的介紹了多種對話框的方法,有興趣的可以了解一下。2016-11-11Android使用ViewPager實現(xiàn)無限滑動效果
相信在大家開發(fā)Android的時候,我們常常用ViewPager來為自己的應用創(chuàng)建廣告條幅,并且常常會遇到ViewPager無限滑動這樣的需求。下面來一起看看吧。2016-09-09Android 仿微信發(fā)動態(tài)九宮格拖拽、刪除功能
這篇文章主要介紹了Android 仿微信發(fā)動態(tài)九宮格拖拽、刪除功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11