Main layout
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Tools:context= ". Mainactivity ">
<relativelayout
Android:id= "@+id/rl"
Android:background= "#F2F2F2"
Android:layout_width= "Match_parent"
android:layout_height= "40dip" >
<radiogroup
Android:id= "@+id/rg"
Android:layout_width= "Match_parent"
android:layout_height= "38dip"
android:orientation= "Horizontal" >
<radiobutton
android:layout_weight= "1"
Android:id= "@+id/zixun"
android:button= "@null"
android:gravity= "Center"
Android:checked= "true"
Android:textcolor= "@drawable/RB_BLUE_BG"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "News"/>
<radiobutton
android:layout_weight= "1"
Android:id= "@+id/redian"
android:button= "@null"
Android:textcolor= "@drawable/RB_BLUE_BG"
android:gravity= "Center"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Hot spot"/>
<radiobutton
android:layout_weight= "1"
Android:id= "@+id/boke"
android:button= "@null"
android:gravity= "Center"
Android:textcolor= "@drawable/RB_BLUE_BG"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "blog"/>
<radiobutton
Android:textcolor= "@drawable/RB_BLUE_BG"
android:layout_weight= "1"
Android:id= "@+id/tuijian"
android:button= "@null"
android:gravity= "Center"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Recommended"/>
</RadioGroup>
<imageview
Android:id= "@+id/img"
Android:layout_width= "1dip"
android:layout_height= "5dip"
Android:layout_alignparentbottom= "true"
Android:background= "#0f0"
Android:scaletype= "Matrix"
/>
</RelativeLayout>
<android.support.v4.view.viewpager
Android:id= "@+id/vp"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:layout_below= "@id/rl" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
Create selector selector RB_BLUE_BG
<selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_checked="true" android:color="#0f0 " /> <item android:state_checked="false" android: color="#000000"/></selector>
Activity
VP =(Viewpager) Findviewbyid (R.ID.VP); RG=(Radiogroup) Findviewbyid (R.ID.RG); IMG=(ImageView) Findviewbyid (r.id.img);//Tabfragmentpageradapter Madapter =NewTabfragmentpageradapter (Getsupportfragmentmanager ()); Vp.setadapter (Madapter); Displaymetrics DM=NewDisplaymetrics (); //put some information about the current window in the Displaymetrics class,Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); //width of the RadioButtonIndicatorwidth = dm.widthpixels/4; Layoutparamsparams=Img.getlayoutparams (); params. width=Indicatorwidth; Img.setlayoutparams (params); Setlisenten ();
Private voidSetlisenten () {Vp.setonpagechangelistener (NewOnpagechangelistener () {@Override Public voidOnpageselected (intposition) { if(rg!=NULL&& Rg.getchildcount () >position) {(RadioButton) Rg.getchildat (position)). PerformClick (); }} @Override Public voidOnpagescrolled (intARG0,floatArg1,intarg2) {} @Override Public voidOnpagescrollstatechanged (intarg0) { } }); Rg.setoncheckedchangelistener (NewOncheckedchangelistener () {Private intCurrentindicatorleft; @Override Public voidOnCheckedChanged (Radiogroup Group,intCheckedid) { Switch(checkedid) { Caser.id.zixun:setimg (0); Break; Caser.id.redian:setimg (1); Break; Caser.id.boke:setimg (2); Break; Caser.id.tuijian:setimg (3); Break; default: Break; } } Private voidSetimg (intCheckedid) {translateanimation animation=Newtranslateanimation (Currentindicatorleft, (Radiobutto N) rg.getchildat (Checkedid)). GetLeft (), 0f, 0f); Animation.setduration ( -); Animation.setfillafter (true); //Performing displacement animationsimg.startanimation (animation); Vp.setcurrentitem (Checkedid); //Viewpager follow together switch//records the distance from the leftmost side of the current subscriptCurrentindicatorleft =((RadioButton) Rg.getchildat (Checkedid)). GetLeft (); } }); }
In fragment
false ); = (TextView) Rootview.findviewbyid (r.id.tv_tabname); = getarguments (); "" )); return Rootview;
Adapter
classTabfragmentpageradapter extends fragmentpageradapter{Privatefragments ft; PublicTabfragmentpageradapter (fragmentmanager FM) {super (FM); //TODO auto-generated Constructor stub} @Override PublicFragment GetItem (intarg0) {ft=Newfragments (); Bundle args=NewBundle (); Args.putstring (Arguments_name, tabtitle[arg0]); Ft.setarguments (args); returnft; } @Override Public intGetCount () {//TODO auto-generated Method Stub returntabtitle.length; } }
RadioButton and Viewpager linkage