This article attempts to imitate the implementation of the Android title display hidden function, by giving ListView set Mlistview.setontouchlistener listening rewrite Ontouch method to monitor the coordinates of the finger movement, When more than Viewconfiguration.get (this). Getscaledtouchslop (); The height of the toubar. When sliding up above this height shows Touba sliding down hidden.
Effect Chart:
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);
The slide shows TitleBar Showhidetitlebar (true);
else if (Mfirsty-mcurrenty > Mtouchslop) {//Up-Slide hide 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 ()) {Manimat
Ortitle.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;
}
}
Layout
<?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>
I hope this article will help you learn about Android software programming.