服务器之家

服务器之家 > 正文

Android实现标题显示隐藏功能

时间:2021-06-07 16:39     来源/作者:阿冰的学习日记

本文实例尝试模仿实现android标题显示隐藏功能,通过给listview设置 mlistview.setontouchlistener 监听 重写ontouch方法 监听手指一动的坐标,当超过viewconfiguration.get(this).getscaledtouchslop(); toubar的高度 .当向上滑动超过这个高度显示touba 向下滑动隐藏。

效果图:

Android实现标题显示隐藏功能

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软件编程有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部