這篇文章主要介紹了如何使用ListView滑動隱藏顯示ToolBar,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
專注于為中小企業(yè)提供網(wǎng)站制作、網(wǎng)站設(shè)計服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)德安免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
引言
在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)這個效果的關(guā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之前進行設(shè)置 */ initHeadView(); listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, datas)); //判斷認(rèn)為是滑動的最小距離(乘以系數(shù)調(diào)整滑動靈敏度) 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(); //向下滑動 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í)行,導(dǎo)致的后果是ListView無法滑動 } }); } /** * 設(shè)置頭布局,注意:這個頭布局的高度要和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)解釋的很詳細(xì)了。唯一需要注意的是:scaledTouchSlop值默認(rèn)獲取的是Android系統(tǒng)能識別的最小滑動距離。我們通過乘以相關(guān)系數(shù),可以適當(dāng)?shù)恼{(diào)整滑動的靈敏度。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何使用ListView滑動隱藏顯示ToolBar”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
本文名稱:如何使用ListView滑動隱藏顯示ToolBar
地址分享:http://chinadenli.net/article22/pgogjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、微信公眾號、網(wǎng)站策劃、響應(yīng)式網(wǎng)站、靜態(tài)網(wǎng)站、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)