国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現標題顯示隱藏功能

2020-04-11 10:47:34
字體:
來源:轉載
供稿:網友

本文實例嘗試模仿實現Android標題顯示隱藏功能,通過給listview設置 mListView.setOnTouchListener 監聽 重寫ontouch方法 監聽手指一動的坐標,當超過ViewConfiguration.get(this).getScaledTouchSlop(); toubar的高度 .當向上滑動超過這個高度顯示touba 向下滑動隱藏。

效果圖:

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>

希望本文所述對大家學習Android軟件編程有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 文山县| 辰溪县| 祁连县| 海林市| 海南省| 罗山县| 扎兰屯市| 上杭县| 长海县| 苏尼特右旗| 亚东县| 大荔县| 安西县| 广元市| 恩平市| 莱阳市| 屏南县| 洛浦县| 无为县| 高陵县| 翁牛特旗| 阳高县| 监利县| 临沭县| 杭锦旗| 磐石市| 灵丘县| 紫云| 阜城县| 崇义县| 如皋市| 即墨市| 五河县| 安义县| 营口市| 金塔县| 丰镇市| 崇明县| 台山市| 洛浦县| 琼海市|