广告
返回顶部
首页 > 资讯 > 移动开发 >Android实现标题显示隐藏功能
  • 904
分享到

Android实现标题显示隐藏功能

Android 2022-06-06 09:06:22 904人浏览 泡泡鱼
摘要

本文实例尝试模仿实现Android标题显示隐藏功能,通过给listview设置 mListView.setOnTouchListener 监听 重写ontouch方法 监听手指

本文实例尝试模仿实现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软件编程有所帮助。

您可能感兴趣的文章:Android中隐藏标题栏和状态栏的方法android 动态控制状态栏显示和隐藏的方法实例Android实现隐藏状态栏和标题栏Android隐藏标题状态栏的方法3种Android隐藏顶部状态栏及标题栏的方法


--结束END--

本文标题: Android实现标题显示隐藏功能

本文链接: https://www.lsjlt.com/news/25674.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作