In Main.xml
<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 "> <Horizontalscrollview Android:id= "@+id/sc"Android:layout_width= "Match_parent"Android:layout_height= "40dip"Android:background= "#F2F2F2"Android:scrollbars= "None" > <relativelayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <Radiogroup Android:id= "@+id/rg"Android:layout_width= "Match_parent"Android:layout_height= "38dip"android:orientation= "Horizontal" > </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> </HorizontalScrollView> <Android.support.v4.view.ViewPager Android:id= "@+id/vp"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_below= "@id/sc" > </android.support.v4.view.ViewPager></RelativeLayout>
Fragment XML .... Fragment_selection_common
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical" > <TextView android:layout_width= "300DP"Android:layout_height= "Wrap_content"android:textsize= "18SP"Android:text= "This is Commonuifragment interface"/> <TextView Android:id= "@+id/tv_tabname"Android:layout_margintop= "30DP"Android:layout_width= "Wrap_content"Android:layout_height= "30DP"android:layout_gravity= "Center"android:textsize= "20SP"/></linearlayout>
Selector selector RB_BLUE_BG
<?xml version= "1.0" encoding= "UTF-8"?><selector xmlns:android= "http://schemas.android.com/ Apk/res/android "> <item android:state_checked=" true "android:color=" #5AB0EB "/> <item android: State_checked= "false" android:color= "#000000"/></selector>
RadioButton Button Nav_radiogroup_item.xml
<?xml version= "1.0" encoding= "Utf-8"? ><radiobutton xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" 0dip " android:layout_height=" Fill_parent " Android:background= "#F2F2F2" android:button= "@null" android:checked= "true" android:gravity= "center" android:text= " android:textcolor=" @ DRAWABLE/RB_BLUE_BG " android:textsize=" 14.0dip "/>
Fragment
Packagecom.bwie.test;ImportAndroid.os.Bundle;Importandroid.support.v4.app.Fragment;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TextView; Public classCommonuifragmentextendsFragment {@Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { View Rootview= Inflater.inflate (R.layout.fragment_selection_common, container,false); TextView Tv_tabname=(TextView) Rootview.findviewbyid (r.id.tv_tabname); Bundle Bundle=getarguments (); Tv_tabname.settext (Bundle.getstring (Mainactivity.arguments_name,"")); returnRootview; } @Override Public voidonactivitycreated (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. onactivitycreated (savedinstancestate); } }
In activity
Packagecom.bwie.test;ImportAndroid.os.Bundle;Importandroid.support.v4.app.Fragment;Importandroid.support.v4.app.FragmentActivity;ImportAndroid.support.v4.app.FragmentManager;ImportAndroid.support.v4.app.FragmentPagerAdapter;ImportAndroid.support.v4.view.ViewPager;ImportAndroid.support.v4.view.ViewPager.OnPageChangeListener;ImportAndroid.util.DisplayMetrics;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup.LayoutParams;Importandroid.view.animation.TranslateAnimation;ImportAndroid.widget.HorizontalScrollView;ImportAndroid.widget.ImageView;ImportAndroid.widget.RadioButton;ImportAndroid.widget.RadioGroup;ImportAndroid.widget.RadioGroup.OnCheckedChangeListener; Public classMainactivityextendsfragmentactivity { Public Static FinalString Arguments_name =NULL; PrivateViewpager VP; PrivateRadiogroup RG; PrivateImageView img; Private intIndicatorwidth; Public StaticString[] Tabtitle = {"Option 1", "Option 2", "option 3", "Option 4", "option 5"};//title PrivateLayoutinflater Inflater; PrivateHorizontalscrollview SC; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); VP=(Viewpager) Findviewbyid (R.ID.VP); RG=(Radiogroup) Findviewbyid (R.ID.RG); IMG=(ImageView) Findviewbyid (r.id.img); SC=(Horizontalscrollview) Findviewbyid (R.id.sc); Initview (); 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) { if(Rg.getchildat (checkedid)! =NULL){//New Translateanimation (Fromxdelta, Toxdelta, Fromydelta, Toydelta);translateanimation animation =Newtranslateanimation (Currentindicatorleft, (RadioButton) rg.ge Tchildat (Checkedid)). GetLeft (), 0f, 0f);//Animation.setinterpolator (New Linearinterpolator ());Animation.setduration (100); 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 (); Sc.smoothscrollto ((Checkedid> 1? ((RadioButton) Rg.getchildat (Checkedid)). GetLeft (): 0)-((RadioButton) Rg.getchildat (2)). GetLeft (), 0); } } }); } Private voidInitview () {//The Displaymetrics class provides a general information about the display, such as display size, resolution, and FontDisplaymetrics DM =NewDisplaymetrics (); //put some information about the current window in the Displaymetrics class,Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); //width of the RadioButtonIndicatorwidth = DM.WIDTHPIXELS/4; Layoutparams params=Img.getlayoutparams (); Params.width=Indicatorwidth; Img.setlayoutparams (params); //get the layout fillerInflater = Layoutinflater.from ( This); INITNAVIGATIONHSV (); Tabfragmentpageradapter Madapter=NewTabfragmentpageradapter (Getsupportfragmentmanager ()); Vp.setadapter (Madapter); } Private voidINITNAVIGATIONHSV () {rg.removeallviews (); for(inti = 0; i < tabtitle.length; i++) {RadioButton RB= (RadioButton) inflater.inflate (R.layout.nav_radiogroup_item,NULL); Rb.setid (i); Rb.settext (Tabtitle[i]); Rb.setlayoutparams (Newlayoutparams (Indicatorwidth, layoutparams.match_parent)); Rg.addview (RB); } } classTabfragmentpageradapterextendsfragmentpageradapter{Privatecom.bwie.test.CommonUIFragment ft; PublicTabfragmentpageradapter (fragmentmanager FM) {Super(FM); //TODO auto-generated Constructor stub} @Override PublicFragment GetItem (intarg0) {ft=Newcommonuifragment (); Bundle args=NewBundle (); Args.putstring (Arguments_name, tabtitle[arg0]); Ft.setarguments (args); returnft; } @Override Public intGetCount () {//TODO auto-generated Method Stub returntabtitle.length; } }}
ScrollView and Viewpager linkage