Android Implementation title display hidden features _android

Source: Internet
Author: User
Tags gety

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.