Android Viewpager+tabhost for Home navigation

Source: Internet
Author: User

Today hair is tabhost combined with Viewpager to achieve the bottom of the navigation, although there are many online demo, but, I still want to write their own practice to send out, yes! is so willful;
First up, as follows:

There are comments in the code, not too much to explain, say a few points to pay attention to the problem
1:tabhost, Tabwidget, framelayout must add ID this attribute, otherwise will error
Android:id= "@android: Id/tabhost"
Android:id= "@android: Id/tabcontent"
Android:id= "@android: Id/tabs"
This property is fixed.
2:viewpager adapter to inherit Pageradapter, do not complete the wrong;
Layout file xml:

 1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <tabhost xmlns:android= "http://schemas.android.com/apk/res/ Android "3 xmlns:tools=" Http://schemas.android.com/tools "4 android:id=" @android: Id/tabhost "5 android:layout_ Width= "Match_parent" 6 android:layout_height= "match_parent" 7 tools:context= "Com.example.wgh.tabhostwithviewpager. Mainactivity "> 8 9 <linearlayout10 android:layout_width=" Match_parent "one android:layout_height= "Match_parent" android:orientation= "vertical" >13 <framelayout15 android:id= "@androi             D:id/tabcontent "android:layout_width=" match_parent "android:layout_height=" Match_parent "18 Android:layout_weight= "1.0" >19 <android.support.v4.view.viewpager21 Android : id= "@+id/viewpager" android:layout_width= "Match_parent" android:layout_height= Parent "></android.support.v4. View.             viewpager>24 </framelayout>26 <tabwidget28 android:id= "@android: Id/tabs" 29 Android:layout_width= "Match_parent" android:layout_height= "Match_parent" Android:la yout_weight= "0.0" android:visibility= "Gone"/>33 <view35 android:layout_width= " Match_parent "android:layout_height=" 1DP "Notoginseng android:background=" #0ff0f0 "/>38 < RADIOGROUP40 android:id= "@+id/radiogroup" android:layout_width= "Match_parent"                 oid:layout_height= "wrap_content" android:orientation= "Horizontal" >45-<radiobutton47 Android:id= "@+id/radiobutton1" android:layout_width= "0DP" Android:layo                 ut_height= "Wrap_content" android:layout_margin= "5DP" android:layout_weight= "1" 52 Android: background= "@drawable/img_draw_money_fail" android:button= "@null" Android:paddingleft = "10DP"/>55 <radiobutton57 android:id= "@+id/radiobutton2" with Android:                 Layout_width= "0DP" android:layout_height= "wrap_content" android:layout_margin= "5DP" 61                 android:layout_weight= "1" android:background= "@drawable/img_draw_money_fail" 63                 android:button= "@null"/>64 <radiobutton66 android:id= "@+id/radiobutton3" 67 Android:layout_width= "0DP" android:layout_height= "Wrap_content" Android: Layout_margin= "5DP" android:layout_weight= "1" android:background= "@drawable/img_draw_m Oney_fail "android:button=" @null "/>73 <radiobutton75 android:id=" @     +id/radiobutton4 "76            Android:layout_width= "0DP" android:layout_height= "Wrap_content" Android:la Yout_margin= "5DP" android:layout_weight= "1" android:background= "@drawable/img_draw_mon Ey_fail "Bayi android:button=" @null "/>82 </radiogroup>83 </linearlayout>84 </t Abhost>

Activity:

  1 package Com.example.wgh.tabhostwithviewpager;  2 3 Import android.app.TabActivity;  4 Import Android.os.Bundle;  5 Import Android.support.v4.view.ViewPager;  6 Import Android.view.LayoutInflater;  7 Import Android.view.View;  8 Import Android.widget.RadioButton; 9 Import Android.widget.RadioGroup; Ten import Android.widget.TabHost; Import java.util.ArrayList;  public class Mainactivity extends Tabactivity {private tabhost tabhost = null; + Private Viewpager Viewpager = null; Private Radiogroup radiogroup = null; Private arraylist<view> list = null; Private View view1 = null; Private View view2 = null; Private View view3 = null; Private View view4 = null; Private RadioButton radioButton1 = null; Private RadioButton radioButton2 = null; $ private RadioButton RadioButton3 = null; Private RadioButton radioButton4 = null; @Override protected void onCreate (Bundle saVedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); 33 Initview (); InitData (); 35 36//Set initialization default option PNS Radiogroup.check (r.id.radiobutton1); Radiobutton1.setbackgroundresource (r.drawable.img_draw_money_success); Viewpager.setcurrentitem (0); Tabhost.setcurrenttab (0); //getviewpager Add Adapter Myadapter adapter = new Myadapter (list); Viewpager.setadapter (adapter); /** * Viewpager Set slide monitor, set the style of the Tabhost tab option based on the position of the Viewpager selected page */Viewpager.seto Npagechangelistener (New Viewpager.onpagechangelistener () {Wuyi @Override public void Onpagescrol             LED (int position, float positionoffset, int positionoffsetpixels) {@Override 56                public void onpageselected (int position) {$ if (position = = 0) {58     Radiobutton1.setbackgroundresource (r.drawable.img_draw_money_success); Radiobutton2.setbackgroundresource (R.drawable.img_draw_money_fail); Radiobutton3.setbackgroundresource (R.drawable.img_draw_money_fail); Radiobutton4.setbackgroundresource (R.drawable.img_draw_money_fail); }else if (position = = 1) {Radiobutton1.setbackgroundresource (r.drawable.img_draw_ Money_fail); Radiobutton2.setbackgroundresource (r.drawable.img_draw_money_success); Radiobutton3.setbackgroundresource (R.drawable.img_draw_money_fail); Radiobutton4.setbackgroundresource (R.drawable.img_draw_money_fail); }else if (position = = 2) {Radiobutton1.setbackgroundresource (r.drawable.img_draw_ Money_fail); Radiobutton2.setbackgroundresource (R.drawable.img_draw_money_fail);            70         Radiobutton3.setbackgroundresource (r.drawable.img_draw_money_success); Radiobutton4.setbackgroundresource (R.drawable.img_draw_money_fail); }else if (position = = 3) {Radiobutton1.setbackgroundresource (r.drawable.img_draw_ Money_fail); Radiobutton2.setbackgroundresource (R.drawable.img_draw_money_fail); Radiobutton3.setbackgroundresource (R.drawable.img_draw_money_fail); Radiobutton4.setbackgroundresource (r.drawable.img_draw_money_success);  The onpagescrollstatechanged (int) of the @Override Bayi public void State) {82} 83});  84 85/** 86 * Set the listener to Radiogroup to change the Viewpager page. */Radiogroup.setoncheckedchangelistener (new Radiogroup.oncheckedchangelistener () {@Override-public void oncheckedchanged (Radiogroup ra) Diogroup,int checkedid) {checkedid) {r.id.radiobutton1:93 case Viewpager.setcurrentitem (0); 94 Radiobutton1.setbackgroundresource (r.drawable.img_draw_money_success); Radiobutton2.setbackgroundresource (R.drawable.img_draw_money_fail); Radiobutton3.setbackgroundresource (R.drawable.img_draw_money_fail); Radiobutton4.setbackgroundresource (R.drawable.img_draw_money_fail); 98 break;                         Case r.id.radiobutton2:100 Viewpager.setcurrentitem (1); 101 Radiobutton1.setbackgroundresource (R.drawable.img_draw_money_fail); 102 Radiobutton2.setbac Kgroundresource (r.drawable.img_draw_money_success); 103 Radiobutton3.setbackgroundresource (R.drawab Le.img_draw_money_fail); 104 RadiobUtton4.setbackgroundresource (r.drawable.img_draw_money_fail); break;106 CAs E r.id.radiobutton3:107 Viewpager.setcurrentitem (2); 108 Radiobutton1.setba Ckgroundresource (R.drawable.img_draw_money_fail); 109 Radiobutton2.setbackgroundresource (R.drawable . Img_draw_money_fail); Radiobutton3.setbackgroundresource (r.drawable.img_draw_money_success); 11 1 Radiobutton4.setbackgroundresource (R.drawable.img_draw_money_fail);                         reak;113 Case r.id.radiobutton4:114 Viewpager.setcurrentitem (3); 115 Radiobutton1.setbackgroundresource (R.drawable.img_draw_money_fail); RadioButton2. Setbackgroundresource (R.drawable.img_draw_money_fail); 117 Radiobutton3.setbackgroundresource (R.dra Wable.img_draw_money_fail); 118 Radiobutton4.setbackgroundresource (r.drawable.img_draw_money_success); 119     break;120}121}122}); 123}124 125/**126 * Initialize data source 127 */128 private void InitData () {129 list = new arraylist<view> (); List.add (view1); 131 list.a     DD (VIEW2); List.add (VIEW3); 133 List.add (VIEW4); 134}135 136/**137 * Initialize control 138 */139 private void Initview () {tabhost = Gettabhost (); 141 142 Viewpager = (Viewpager) Findviewbyid (r.id. Viewpager); 143 Radiogroup = (radiogroup) Findviewbyid (r.id.radiogroup); 144 RadioButton1 = (RadioButton) fi Ndviewbyid (R.id.radiobutton1); 145 radioButton2 = (RadioButton) Findviewbyid (r.id.radiobutton2); 146 Radiobu Tton3 = (RadioButton) Findviewbyid (R.id.radiobutton3); 147 RadioButton4 = (RadioButton) Findviewbyid (R.id.radiobutt    ON4); 148/**149  * The layout instance to be shown on each page is added to the list, and the corresponding layout150 */151 layoutinflater inflater = Layoutinflate will be shown by the adapter return. R.from (this), View1 = Inflater.inflate (r.layout.layout_one,null); 153 View2 = Inflater.inflate (r.layout. Layout_two,null); 154 view3 = Inflater.inflate (r.layout.layout_three,null); 155 view4 = Inflater.inflate (R.l Ayout.layout_four,null); 156}157 158}

Viewpager Adapter Myadapter:

1 public class Myadapter extends Pageradapter {2     private arraylist<view> list = null; 3  4 public     Myadap ter (arraylist<view> list) {5         this.list = list; 6     } 7  8     @Override 9 public     int GetCount () {10
   return list.size ();     }12 @Override14 public     boolean isviewfromobject (View arg0, Object arg1) {15< C12/>return arg0 = = arg1;16     }17     @Override19 public     Object instantiateitem (viewgroup container, int Position) {         Container.addview (list.get (position)),         return List.get (position),     }23     @ Override24 public     void Destroyitem (ViewGroup container, int position, object object)         Container.removeview (List.get (position));     }29}

If you have any questions, please leave a message!

Android Viewpager+tabhost for Home navigation

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.