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編程開發(fā)錄音和播放錄音簡(jiǎn)單示例
這篇文章主要介紹了Android編程開發(fā)錄音和播放錄音的方法,結(jié)合實(shí)例形式分析了Android多媒體開發(fā)中音頻操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08
Android實(shí)現(xiàn)注冊(cè)頁(yè)面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)注冊(cè)頁(yè)面之監(jiān)聽器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android控件BottomSheet實(shí)現(xiàn)底邊彈出選擇列表
這篇文章主要介紹了Android控件BottomSheet實(shí)現(xiàn)底邊彈出選擇列表,比較常用的選擇條件或跳轉(zhuǎn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android實(shí)現(xiàn)調(diào)用攝像頭拍照與視頻功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)調(diào)用攝像頭拍照與視頻功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Android使用SqLite實(shí)現(xiàn)登錄注冊(cè)功能流程詳解
這篇文章主要介紹了使用Android Studio自帶的sqlite數(shù)據(jù)庫(kù)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的登錄注冊(cè)功能,SQLite是一個(gè)軟件庫(kù),實(shí)現(xiàn)了自給自足的、無(wú)服務(wù)器的、零配置的、事務(wù)性的SQL數(shù)據(jù)庫(kù)引擎,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Android開發(fā)之Adobe flash操作工具類
這篇文章主要介紹了Android開發(fā)之Adobe flash操作工具類,可實(shí)現(xiàn)flash的安裝及判斷flash是否安裝等功能,需要的朋友可以參考下2017-12-12

