Android實(shí)現(xiàn)標(biāo)題顯示隱藏功能
本文實(shí)例嘗試模仿實(shí)現(xiàn)Android標(biāo)題顯示隱藏功能,通過給listview設(shè)置 mListView.setOnTouchListener 監(jiān)聽 重寫ontouch方法 監(jiān)聽手指一動(dòng)的坐標(biāo),當(dāng)超過ViewConfiguration.get(this).getScaledTouchSlop(); toubar的高度 .當(dāng)向上滑動(dòng)超過這個(gè)高度顯示touba 向下滑動(dòng)隱藏。
效果圖:

package com.example.hidetitlebardemo;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
private ListView mListView;
private RelativeLayout mTitle;
private int mTouchSlop;
private SimpleAdapter mAdapter;
private float mFirstY;
private float mCurrentY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();
initViews();
showHideTitleBar(true);
}
private void initViews() {
mListView = (ListView) findViewById(R.id.id_lv);
mTitle = (RelativeLayout) findViewById(R.id.id_title);
mAdapter = new SimpleAdapter(this, getData(),
R.layout.lv_item,
new String[]{"info"},
new int[]{R.id.num_info});
mListView.setAdapter(mAdapter);
mListView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mFirstY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
mCurrentY = event.getY();
if (mCurrentY - mFirstY > mTouchSlop) {
System.out.println("mtouchislop:" + mTouchSlop);
// 下滑 顯示titleBar
showHideTitleBar(true);
} else if (mFirstY - mCurrentY > mTouchSlop) {
// 上滑 隱藏titleBar
showHideTitleBar(false);
}
break;
case MotionEvent.ACTION_UP:
break;
}
return false;
}
});
}
private Animator mAnimatorTitle;
private Animator mAnimatorContent;
private void showHideTitleBar(boolean tag) {
if (mAnimatorTitle != null && mAnimatorTitle.isRunning()) {
mAnimatorTitle.cancel();
}
if (mAnimatorContent != null && mAnimatorContent.isRunning()) {
mAnimatorContent.cancel();
}
if (tag) {
mAnimatorTitle = ObjectAnimator.ofFloat(mTitle, "translationY", mTitle.getTranslationY(), 0);
mAnimatorContent = ObjectAnimator.ofFloat(mListView, "translationY", mListView.getTranslationY(), getResources().getDimension(R.dimen.title_height));
} else {
mAnimatorTitle = ObjectAnimator.ofFloat(mTitle, "translationY", mTitle.getTranslationY(), -mTitle.getHeight());
mAnimatorContent = ObjectAnimator.ofFloat(mListView, "translationY", mListView.getTranslationY(),0);
}
mAnimatorTitle.start();
mAnimatorContent.start();
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> data = new ArrayList<>();
for (int i = 'A'; i < 'z'; i++) {
Map<String, Object> map = new HashMap<>();
map.put("info", (char) i);
data.add(map);
}
return data;
}
}
布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <RelativeLayout android:id="@+id/id_title" android:background="#00ccff" android:layout_width="match_parent" android:layout_height="40dp"> <TextView android:text="Ace " android:layout_centerInParent="true" android:textSize="22sp" android:textColor="#ffffff" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> <ListView android:id="@+id/id_lv" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
希望本文所述對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。
相關(guān)文章
Android編程實(shí)現(xiàn)下載圖片及在手機(jī)中展示的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)下載圖片及在手機(jī)中展示的方法,涉及Android針對(duì)圖形文件的遠(yuǎn)程下載及遍歷顯示相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
Android開發(fā)之進(jìn)度條ProgressBar的示例代碼
本篇文章主要介紹了Android開發(fā)之進(jìn)度條ProgressBar的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Android?ASM插樁探索實(shí)戰(zhàn)詳情
這篇文章主要介紹了Android?ASM插樁探索實(shí)戰(zhàn)詳情,文章圍繞主題展開詳細(xì)的內(nèi)容戒殺,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
Flutter StreamBuilder組件實(shí)現(xiàn)局部刷新示例講解
日常使用最多的局部刷新為Provider狀態(tài)管理 Selector,今天分享flutter框架自帶的StreamBuilder組件,該組件可做到局部刷新,使用簡(jiǎn)單且輕便2022-11-11
Android中使用Gradle來構(gòu)建App項(xiàng)目的入門指南
Gradle是Java世界中一個(gè)高人氣自動(dòng)化構(gòu)建工具,在安卓開發(fā)領(lǐng)域同樣備受追捧,這里為大家?guī)鞟ndroid中使用Gradle來構(gòu)建App項(xiàng)目的入門指南,來看看Gradle的作用與基本結(jié)構(gòu).2016-06-06
Android中使用ListView實(shí)現(xiàn)漂亮的表格效果
這篇文章主要介紹了Android中使用ListView實(shí)現(xiàn)漂亮的表格效果,本文用詳細(xì)的代碼實(shí)例創(chuàng)建了一個(gè)股票行情表格,需要的朋友可以參考下2014-10-10
解決Android BitmapFactory的基本使用問題
很多朋友給小編反饋使用方法BitmapFactory.decodeFile轉(zhuǎn)化Bitmap時(shí)報(bào)錯(cuò),究竟是什么原因?qū)е洛e(cuò)誤問題呢?今天通過本文給大家介紹下解決Android BitmapFactory的基本使用問題,感興趣的朋友一起看看吧2021-10-10
Android中ViewFlipper和AdapterViewFlipper使用的方法實(shí)例
ViewFlipper和AdapterViewFlipper是Android自帶的一個(gè)多頁面管理控件,下面這篇文章主要給大家介紹了關(guān)于Android中ViewFlipper和AdapterViewFlipper使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05

