Androidスライド可能なTabページ

7086 ワード

RadioGroup+ViewPager+Fragmentで実現したスライド可能なTabページは、非常に柔軟です.
最終効果図
Android可滑动的Tab页_第1张图片   Android可滑动的Tab页_第2张图片
demoダウンロード:http://download.csdn.net/download/shenyuanqing/9064189
部分ソース
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);
        //       
        initView();
        //   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     
        viewPager.setAdapter(new myFragmentPagerAdapter(getSupportFragmentManager(), alFragment));
        //ViewPager     Fragment
        viewPager.setCurrentItem(0);
        //ViewPager      
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            //  ViewPager,RadioButton         
            @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) {

            }
        });
    }

    /**
     *   RadioButton  ViewPager    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