Android Sliding tab page

Source: Internet
Author: User

The Sliding tab page implemented by Radiogroup+viewpager+fragment is very flexible.

Eventually


Demo Download: http://download.csdn.net/download/shenyuanqing/9064189

Part of the source

Mainactivity

Package Com.example.shen.tabtest.activity;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentactivity;import Android.support.v4.view.viewpager;import Android.widget.radiobutton;import Android.widget.radiogroup;import Com.example.shen.tabtest.r;import Com.example.shen.tabtest.adapter.myfragmentpageradapter;import Com.example.shen.tabtest.fragment.evaluationfragment;import com.example.shen.tabtest.fragment.MerchantFragment; Import Com.example.shen.tabtest.fragment.orderfragment;import Java.util.arraylist;public class MainActivity extends    Fragmentactivity implements radiogroup.oncheckedchangelistener{private Viewpager Viewpager;    Private Radiogroup Radiogroup;    Private RadioButton rborder,rbevaluation,rbmerchant;    Private arraylist<fragment> alfragment;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Initializing the interfaceComponent Initview ();    Initialize Viewpager Initviewpager ();        } private void Initview () {viewpager= (Viewpager) Findviewbyid (R.id.viewpager);        radiogroup= (Radiogroup) Findviewbyid (R.id.radiogroup);        Rborder= (RadioButton) Findviewbyid (R.id.rb_order);        rbevaluation= (RadioButton) Findviewbyid (r.id.rb_evaluation);        Rbmerchant= (RadioButton) Findviewbyid (r.id.rb_merchant);    Radiogroup.setoncheckedchangelistener (this);        } private void Initviewpager () {orderfragment orderfragment=new orderfragment ();        Evaluationfragment evaluationfragment=new evaluationfragment ();        Merchantfragment merchantfragment=new merchantfragment ();        Alfragment=new arraylist<> ();        Alfragment.add (orderfragment);        Alfragment.add (evaluationfragment);        Alfragment.add (merchantfragment);        Viewpager Setting Adapter Viewpager.setadapter (New Myfragmentpageradapter (Getsupportfragmentmanager (), alFragment)); Viewpager DisplayShow the first fragment Viewpager.setcurrentitem (0);            Viewpager page Toggle Listener viewpager.setonpagechangelistener (New Viewpager.onpagechangelistener () {@Override            public void onpagescrolled (int position, float positionoffset, int positionoffsetpixels) {}                Slide Viewpager,radiobutton The selected state to make the corresponding transformation @Override public void onpageselected (int position) {                        Switch (position) {case 0:radiogroup.check (r.id.rb_order);                    Break                        Case 1:radiogroup.check (r.id.rb_evaluation);                    Break                        Case 2:radiogroup.check (r.id.rb_merchant);                Break    }} @Override public void onpagescrollstatechanged (int state) {}}); }/** * Click RadioButton Toggle Viewpager in the corresponding fragment * @param grouP * @param checkedid */@Override public void oncheckedchanged (radiogroup group, int checkedid) {SWI                TCH (checkedid) {case R.id.rb_order:viewpager.setcurrentitem (0,false);            Break                Case R.id.rb_evaluation:viewpager.setcurrentitem (1,false);            Break                Case R.id.rb_merchant:viewpager.setcurrentitem (2,false);        Break }    }}
Activity_main.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent "Android:layout_height=" match_parent "android:background=" @color/white "> <radiogroup android:id=" @+ Id/radiogroup "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android:orien tation= "Horizontal" android:layout_margintop= "10DP" > <radiobutton android:id= "@+id/rb_order "Android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:text="            @string/order "android:checked=" true "style=" @style/style_radiobutton "/> <radiobutton Android:id= "@+id/rb_evaluation" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/evaluation" style= "@style/style_radiobutton"/> <ra Diobutton Android:id= "@+id/rb_merchant" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "@string/merchant" style= "@style/style_radiobutton"/> </RadioGroup> <android . Support.v4.view.ViewPager android:id= "@+id/viewpager" android:layout_width= "Match_parent" Android:la yout_height= "Match_parent" android:layout_below= "@id/radiogroup" > </android.support.v4.view.viewpager>& Lt;/relativelayout>
Myfragmentpageradapter

Package Com.example.shen.tabtest.adapter;import Android.support.v4.app.fragment;import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmentpageradapter;import java.util.arraylist;/** * Created by Administrator on 2015/6/24. */public class Myfragmentpageradapter extends Fragmentpageradapter {    private arraylist<fragment> list;    Public Myfragmentpageradapter (fragmentmanager FM, arraylist<fragment> list) {        super (FM);        this.list=list;    }    @Override public    Fragment getItem (int position) {        return list.get (position);    }    @Override public    int GetCount () {        return list.size ();}    }
Style_radiobutton

    <style name= "Style_radiobutton" >        <item name= "android:layout_weight" >1</item>        <item Name= "Android:button" > @null </item>        <item name= "Android:background" > @null </item>        <item name= "android:gravity" >center</item>        <item name= "android:drawablepadding" >10dp</ item>        <item name= "Android:drawablebottom" > @drawable/radiobutton_selector</item>        <item Name= "Android:textcolor" > @drawable/textcolor_selector</item>        <item name= "Android:textsize" > 16sp</item>    </style>


















Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Sliding tab page

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.