Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android可滑動的Tab頁

Android可滑動的Tab頁

編輯:關於Android編程

RadioGroup+ViewPager+Fragment實現的可滑動的Tab頁,非常的靈活。

最終效果圖

\ \

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

 

 






 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved