This article mainly addresses the implementation of custom Tabhost, as well as integration of dynamic Add tab functionality via code, TAB horizontal Auto-swipe, and toggle tab functionality with gestures.
Follow me to complete this perfect solution:
1. Define the layout of the Tabwidget tab: Tab_button.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" 119dip " android:layout_height=" wrap_content " android:orientation=" Vertical "> <textview android:id=" @+id/tv_tabs_tabhost " android:layout_width=" 119dip " android:layout_height= "39dip" android:gravity= "center" android:text= "Tab1"/></linearlayout>
2. layout file for Main page tab control: Activity_tabhost.xml
<tabhost android:id= "@+id/tabhost" android:layout_width= "Match_parent" Android : layout_height= "Wrap_content" > <linearlayout android:layout_width= "match_parent" android:layout_height= "wrap_content" android:orientation= "vertical" > <!--horizontal scrolling --<relativelayout android:layout_width= "Fill_parent" Android : layout_height= "Wrap_content" >
3, the main surface Mytabhostactivity.javaFirst define the following variables:
Tabhostprivate static Tabhost tbproducthost;//swipe gesture private gesturedetector detector;//tab widget horizontal slider private Horizontalscrollview hscroller;private int screenwidth;//screen width unit: DP
One of the OnCreate methods:public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_tabhost);//Initialize Tabhosttbproducthost = (tabhost) Findviewbyid (r.id.tabhost); TbProductHost.setup ( CaptureMultiActivity.this.getLocalActivityManager ());//Get the phone screen wide displaymetrics displaymetrics = new Displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (displaymetrics); screenwidth = Methods.px2dip ( Capturemultiactivity.this,displaymetrics.widthpixels);//dp//Initialize Tabhost, according to arrfor (int i=0;i<10;i++) {View view = Layoutinflater.from (Mytabhostactivity.this). Inflate (R.layout.tab_button, NULL); TextView TView = (TextView) View.findviewbyid (r.id.tv_tabs_tabhost); Tview.settext ("tab" +i); Tbproducthost.addtab (" Tab "+i). Setindicator (view). SetContent (Mytabhostactivity.this); Updatetab (tbproducthost);// Call method to set the color of the Tabwidget tab Tbproducthost.setontabchangedlistener (New Ontabchangedlistener ());} Hscroller = (Horizontalscrollview) Findviewbyid (R.id.hscroller_scan);}
where the Px2dip (int tt) method in the methods class is:
/** * px to dip * * @param context * @param pxvalue * @return */public static int Px2dip (context context, float pxvalue ) {Final Float scale = context.getresources (). Getdisplaymetrics (). Density;return (int) (Pxvalue/scale + 0.5f);}
Where the Updatetab (Tabhost TB) method is used primarily to set the color of the Tabwidget tab, and when selected, the color is as follows:
/** * Update tab label color, and font color * * @param tabhost */private void Updatetab (final tabhost tabhost) {for (int i = 0; i < tab Host.gettabwidget (). Getchildcount (); i++) {View view = Tabhost.gettabwidget (). Getchildat (i); TextView TV = (TextView) tabhost.gettabwidget (). Getchildat (i). Findviewbyid (R.id.tv_tabs_tabhost); Tv.settextsize (16 ); Tv.settypeface (Typeface.serif, 2); Set the font and style if (tabhost.getcurrenttab () = = i) {//Check View.setbackgroundcolor (Getresources (). GetColor (R.color.color_text _red));//Selected Background #eb037ftv. SetTextColor (This.getresources (). Getcolorstatelist (Android. R.color.black));} else {//uncheck View.setbackgroundcolor (Getresources (). GetColor (R.color.color_text_yellow));//non-selectable background # F8c514tv.settextcolor (This.getresources (). Getcolorstatelist (Android. R.color.white));}}}
Where the Ontabchangedlistener class:Class Ontabchangedlistener implements Ontabchangelistener {@Overridepublic void ontabchanged (String tabId) { Tbproducthost.setcurrenttabbytag (TABID); System.out.println ("TabID" + tabid); System.out.println ("Curreny after:" + Tbproducthost.getcurrenttabtag ()); Updatetab (Tbproducthost);}}
Another mytabhostactivity class to implement Tabcontentfactory, Ongesturelistener a total of two interfaces, and implement the method inside:@Overridepublic View createtabcontent (String arg0) {//initializes the contents of one of the tabs in Tabhost, you can load the XML layout file already defined by Inflater /to-doreturn view;} public void Flingleft () {//toggle tab INT currenttab = Tbproducthost.getcurrenttab (); if (Currenttab! = 0) {Currenttab--;switch Tab (Currenttab);} Horizontal sliding Hscrollmanagment (true, Currenttab);} public void Flingright () {//toggle tab INT currenttab = Tbproducthost.getcurrenttab (); if (currenttab! = Tbproducthost.gettabwidget (). Getchildcount ()) {currenttab++;switchtab (currenttab);} Horizontal sliding Hscrollmanagment (false, Currenttab);} Used to automatically center the selected tab when the tab is toggled private void Hscrollmanagment (Boolean istoleft, int currenttab) {int count = Tbproducthost.gettabwidget (). Getchildcount (); System.out.println ("000111:hscrollmanagment count=" + count); if (179 * count > ScreenWidth) {int nextposx = (int) (Curr Enttab + 0.5) * 179-screenwidth/2;//Here 179 can be modified by itself//Hscroller.scrollto (NEXTPOSX, 0); Hscroller.smoothscrollto (NextPosX , 0);}} private static void Switchtab (final int totab) {New Thread (New Runnable () {@Overridepublic void run () {Tbproducthost.post (new Runnable () {@Overridepublic void run () {TbP Roducthost.setcurrenttab (Totab);}}). Start ();} @Overridepublic boolean ontouchevent (Motionevent event) {return this.detector.onTouchEvent (event);} @Overridepublic boolean Ondown (motionevent arg0) {//TODO auto-generated method Stubreturn false;} @Overridepublic boolean onfling (motionevent E1, motionevent E2, float velocityx,float velocityy) {if (E1.getx ()-E2.getx ( ) < -120) {flingleft (); return true;} else if (E1.getx ()-e2.getx () >) {flingright (); return true;} return false;} @Overridepublic void Onlongpress (Motionevent e) {//TODO auto-generated Method stub} @Overridepublic Boolean Onscroll ( Motionevent E1, motionevent E2, float distancex,float distancey) {//TODO auto-generated method Stubreturn false;} @Overridepublic void Onshowpress (Motionevent e) {//TODO auto-generated Method stub} @Overridepublic Boolean Onsingletapup (motionevent e) {//TODO Auto-generatEd method Stubreturn false;}
At this point, a perfect tabhost custom solution is completed.
android--Solution--Custom tabhost (dynamic add option + Auto horizontal swipe tab + gesture toggle tab and content function)