Slidable Tab page for Android

Source: Internet
Author: User

Slidable Tab page for Android

RadioGroup + ViewPager + Fragment provides Flexible Sliding tabs.

Final

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

Some source code

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
 
  
AlFragment; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // initialize the interface component 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. callback (this);} private void initViewPager () {OrderFragment orderFragment = new OrderFragment (); EvaluationFragment evaluationFragment = new EvaluationFragment (); MerchantFragment placement = new callback (); alFragment = new ArrayList <> (); alFragment. add (orderFragment); alFragment. add (evaluationFragment); alFragment. add (merchantFragment); // ViewPager sets the adapter viewPager. setAdapter (new myFragmentPagerAdapter (getsuppfrfragmentmanager (), alFragment); // ViewPager displays the first Fragment viewPager. setCurrentItem (0); // The ViewPager page switches to listener viewPager. setOnPageChangeListener (new ViewPager. onPageChangeListener () {@ Override public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) {}// sliding ViewPager, radioButton changes the selected status. @ 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 to switch the corresponding Fragment * @ param group * @ param checkedId */@ Override public void onCheckedChanged (RadioGroup group, int checkedId) {switch (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

 

 

     
          
           
            
         
     
    
   
          
 
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
 
   list;    public myFragmentPagerAdapter(FragmentManager fm, ArrayList
  
    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

 

 

 





 

 

Related Article

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.