Write an Android-driven tabhost (similar to the Twitter-client switching effect)

Source: Internet
Author: User

First:


This layout is:

<?xml version= "1.0" encoding= "UTF-8"? ><tabhost xmlns:android= "Http://schemas.android.com/apk/res/android    "Android:id=" @android: Id/tabhost "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "> <linearlayout android:layout_width= "fill_parent" android:layout_height= "Fill_parent" Android:orie ntation= "vertical" > <framelayout android:id= "@android: Id/tabcontent" Android:layout_wi Dth= "Fill_parent" android:layout_height= "fill_parent" android:layout_weight= "1.0" > &L T LinearLayout android:id= "@+id/tab_news1" android:layout_width= "Fill_parent" a                    ndroid:layout_height= "fill_parent" android:orientation= "vertical" > <textview Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" an              droid:text= "TAB1"      Android:textsize= "30SP" > </TextView> </LinearLayout> <linear Layout android:id= "@+id/tab_news2" android:layout_width= "Fill_parent" Android                    : layout_height= "fill_parent" android:orientation= "vertical" > <textview Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" Android: text= "Tab2" android:textsize= "30sp" > </TextView> </linearlayout&            Gt                <linearlayout android:id= "@+id/tab_news3" android:layout_width= "Fill_parent"                    android:layout_height= "fill_parent" android:orientation= "vertical" > <textview                    Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" Android: text= "Tab3" android:textsize= "30sp" > </TextView> </linearlayout > <linearlayout android:id= "@+id/tab_news4" android:layout_width= "Fill_pare NT "android:layout_height=" Fill_parent "android:orientation=" vertical "> & Lt                    TextView android:layout_width= "fill_parent" android:layout_height= "Fill_parent"            android:text= "TAB4" android:textsize= "30SP" > </TextView> </LinearLayout> <linearlayout android:id= "@+id/tab_news5" android:l  Ayout_width= "Fill_parent" android:layout_height= "fill_parent" android:orientation= "vertical" > <textview android:layout_width= "fill_parent" Android:layout_ height= "Fill_parent"                   android:text= "Tab5" android:textsize= "30SP" > </TextView>        </LinearLayout> </FrameLayout><!--This is the bottom tab, covered with a imageview, tab_widget_image can be slid--            <framelayout android:id= "@+id/tab_widget_layout" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_gravity= "Bottom" android:background= "@drawa Ble/tab_bottom "> <tabwidget android:id=" @android: Id/tabs "Android:layout_w Idth= "Fill_parent" android:layout_height= "fill_parent"/> <imageview androi D:id= "@+id/tab_widget_image" android:layout_width= "wrap_content" android:layout_height= "fill _parent "android:layout_gravity=" left "android:scaletype=" Fitxy "android:src= "@drawable/tab_bottom_selected "/> </FrameLayout> </LinearLayout></TabHost> 

In the code, initialize it first:

private void Inittabhost () {mtabhost = Gettabhost (); mtabwidget = Mtabhost.gettabwidget (); mtabhost.setcurrenttab (0);/* * Home */view Homewidgetview = mlayoutinflater.inflate (R.layout.tab_indicator, Mtabwidget, false); Intent Intent = new Inten T (); Intent.setclass (this, mainactivity.class); Tabhost.tabspec Tabspec = Mtabhost.newtabspec (Home_tab); TextView Hometitle = (TextView) Homewidgetview.findviewbyid (r.id.title); Hometitle.settext (R.string.main_tab_home) ; ImageView Homeicon = (ImageView) Homewidgetview.findviewbyid (R.id.icon); Homeicon.setbackgroundresource (Normal_ Image[0]); tabspec = Tabspec.setindicator (Homewidgetview). SetContent (intent); Mtabhost.addtab (tabspec);/** chat */View Barwidgetview = Mlayoutinflater.inflate (R.layout.tab_indicator, Mtabwidget, false); Tabspec = This.mTabHost.newTabSpec (chat_tab); intent = new Intent (this, edittextexample.class); TextView Bartitle = (TextView) Barwidgetview.findviewbyid (r.id.title); Bartitle.settext (R.string.main_tab_bar); I Mageview Baricon = (ImageView) BARwidgetview.findviewbyid (R.id.icon); Baricon.setbackgroundresource (normal_image[1]); TabSpec = Tabspec.setindicator (Barwidgetview). SetContent (intent); Mtabhost.addtab (tabspec);/** Message Center */view Msgwidgetview = Mlayoutinflater.inflate (R.layout.tab_indicator, Mtabwidget, false); Tabspec = This.mTabHost.newTabSpec (Message_tab) ; intent = new Intent (this, mainactivity.class); TextView Msgtitle = (TextView) Msgwidgetview.findviewbyid (r.id.title); Msgtitle.settext (R.string.main_tab_message) ; ImageView Msgicon = (ImageView) Msgwidgetview.findviewbyid (R.id.icon); Msgicon.setbackgroundresource (normal_image[ 2]); Tabspec = Tabspec.setindicator (Msgwidgetview). SetContent (intent); Mtabhost.addtab (tabspec);/** My profile */View Myinfowidgetview = Mlayoutinflater.inflate (R.layout.tab_indicator, Mtabwidget, false); Tabspec = This.mTabHost.newTabSpec (profile_tab); intent = new Intent (this, mainactivity.class); TextView Myinfotitle = (TextView) Myinfowidgetview.findviewbyid (r.id.title); Myinfotitle.settext (r.string.main_tab_ MyiNFO); ImageView Myinfoicon = (ImageView) Myinfowidgetview.findviewbyid (R.id.icon); Myinfoicon.setbackgroundresource ( NORMAL_IMAGE[3]); tabspec = Tabspec.setindicator (Myinfowidgetview). SetContent (intent); Mtabhost.addtab (TABSPEC);/* * More */view Morewidgetview = Mlayoutinflater.inflate (R.layout.tab_indicator, Mtabwidget, false); TabSpec = This.mTabHost.newTabSpec (more_tab); intent = new Intent (this, edittextexample.class); TextView Moretitle = (TextView) Morewidgetview.findviewbyid (r.id.title); Moretitle.settext (R.string.main_tab_more) ; ImageView Moreicon = (ImageView) Morewidgetview.findviewbyid (R.id.icon); Moreicon.setbackgroundresource (Normal_ IMAGE[4]); tabspec = Tabspec.setindicator (Morewidgetview). SetContent (intent); Mtabhost.addtab (TABSPEC); Mtabhost.setontabchangedlistener (New Tabhostlistener (this));((ImageView) mtabwidget.getchildat (0). Findviewbyid (    R.id.icon). Setimageresource (Selected_image[0]);((TextView) mtabwidget.getchildat (0). Findviewbyid (R.id.title)) . SetTextColor (TabactivitYWithAnimation.this.getResources (). GetColor (R.color.white)); Mtabwidget.getchildat (0). Setbackgroundresource ( r.drawable.tab_bottom_selected); animimage = (ImageView) Findviewbyid (r.id.tab_widget_image); Animimage.setvisibility (view.invisible);}


The animation effect is raised by sliding movement to indicate which tab is currently switched to:

 private void ShowAnimation () {LOG.V ("test", "showanimation");     if (Lasttabindex = = Currtabindex) {return;     } if (Mtabwidget.getchildcount () < 2) {return; }//This is to support the horizontal screen effect, re-give the child view set width high if (getresources (). GetConfiguration (). Orientation! = Lastimagechangeorientatio                  N) {lastimagechangeorientation = Getresources (). GetConfiguration (). Orientation;                  Widgetitemwidth = Mtabwidget.getwidth ()/Mtabwidget.getchildcount ();         View Currview = Mtabwidget.getchildat (Currtabindex);         Focuswidgetitemwidth = Currview.getwidth ();                  Focuswidgetitemheight = Currview.getheight ();         Layoutparams LP = Animimage.getlayoutparams ();         Lp.width = Focuswidgetitemwidth;         Lp.height = Focuswidgetitemheight;     ANIMIMAGE.SETLAYOUTPARAMS (LP);     } int fromX = Lasttabindex * widgetitemwidth;     int ToX = Currtabindex * widgetitemwidth; LOG.V ("Test", "FromX:" + FromX + "ToX:" + ToX);         translateanimation animation = new Translateanimation (FromX, ToX, 0, 0);         Animation.setduration (600); Animation.setanimationlistener (New Animationlistener () {@Override public void Onanimati OnStart (Animation Animation) {mtabwidget.getchildat (Lasttabindex). Setbackgroundresource (R.drawable.tab_bot                Tom); ((ImageView) Mtabwidget.getchildat (Lasttabindex). Findviewbyid (R.id.icon)). Setimageresource (Normal_image                [Lasttabindex]); ((TextView) Mtabwidget.getchildat (Lasttabindex). Findviewbyid (R.id.title)). SetTextColor (tabactivitywithanimation                . This.getresources (). GetColor (R.drawable.gray2));            Animimage.setvisibility (view.visible);                        } @Override public void Onanimationrepeat (Animation Animation) {} @Override public void Onanimationend (Animation Animation) {animimage.setvisibility (view.invisible); ((ImageView) Mtabwidget.getchildat (Currtabindex). Findviewbyid (R.id.icon)). Setimageresource (Selected_ima                Ge[currtabindex]); ((TextView) Mtabwidget.getchildat (Currtabindex). Findviewbyid (R.id.title)). SetTextColor (Tabactivitywithanima                Tion.this.getResources (). GetColor (R.color.white));                Mtabwidget.getchildat (Currtabindex). Setbackgroundresource (r.drawable.tab_bottom_selected);            Lasttabindex = Currtabindex;         }        });     Animimage.startanimation (animation);  } Private class Tabhostlistener implements Tabhost.ontabchangelistener {context context; public Tabhostlistener (context     Context) {This.context = context;} public void ontabchanged (String paramstring) {lasttabindex = Currtabindex;     Currtabindex = Mtabhost.getcurrenttab ();     if (lasttabindex! = Currtabindex) {showanimation ();  }      } }

Code can be downloaded in http://download.csdn.net/detail/baidu_nod/7614423

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.