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