ViewPager fragment android tab选项卡的使用

MainActivity.java

public class MainActivity extends Activity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private RadioButton rb_my, rb_withdrawals;
    private MyFragment mMyFragment;
    private WithdrawalsFragment mWithdrawalsFragment;
    private ViewPager vp_main;
    private ArrayList mFragmentList;
    private MyViewPagerAdapter mViewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
        findViews();
        setListensers();
        InitView();
    }
    protected void findViews() {
        rb_withdrawals = (RadioButton) findViewById(R.id.rb_withdrawals);
        rb_my = (RadioButton) findViewById(R.id.rb_my);
        vp_main = (ViewPager) findViewById(R.id.vp_main);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_withdrawals:
                vp_main.setCurrentItem(0);
                break;
            case R.id.rb_my:
                vp_main.setCurrentItem(1);
                break;
            default:
                break;
        }
    }

    protected void setListensers() {
        rb_withdrawals.setOnClickListener(this);
        rb_my.setOnClickListener(this);
        vp_main.setOnPageChangeListener(this);
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageSelected(int id) {
        switch (id) {
            case 0:
                rb_withdrawals.setChecked(true);
                break;
            case 1:
                rb_my.setChecked(true);
                break;
            default:
                break;
        }
    }

    private void addFragment() {
        if (mWithdrawalsFragment == null) {
            mWithdrawalsFragment = new WithdrawalsFragment();
        }
        if (mMyFragment == null) {
            mMyFragment = new MyFragment();
        }

        if (mFragmentList == null) {
            mFragmentList = new ArrayList();// 初始化数据
            mFragmentList.add(mWithdrawalsFragment);
            mFragmentList.add(mMyFragment);
        }
    }

    private void setViewPager() {
        if (mViewPagerAdapter == null) {
            mViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), mFragmentList);
            vp_main.setAdapter(mViewPagerAdapter);
            vp_main.setOffscreenPageLimit(3);
        }
    }

    private void InitView() {
        addFragment();
        setViewPager();
    }
}

activity_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" android:background="@color/screen_bg" tools:context=".activity.MainActivity">

    <RadioGroup  android:id="@+id/rg_control_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@color/white" android:orientation="horizontal" android:paddingBottom="10dp" android:paddingTop="10dp">

        <RadioButton  android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/radio_tab_home" android:button="@null" android:textColor="@color/light_gray" android:textSize="12dp" android:visibility="gone"/>

        <RadioButton  android:id="@+id/rb_withdrawals" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/get_red_package_select" android:button="@null" android:textColor="@color/light_gray" android:textSize="12dp" />

        <RadioButton  android:id="@+id/rb_my" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/radio_tab_my" android:button="@null" android:textColor="@color/light_gray" android:textSize="12dp" />
    RadioGroup>

    <android.support.v4.view.ViewPager  android:id="@+id/vp_main" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/rg_control_button" android:flipInterval="30" android:persistentDrawingCache="animation" />

    <View  android:layout_width="match_parent" android:layout_height="1dp" android:layout_below="@id/vp_main" android:background="#60777777" />
RelativeLayout>

转载于:https://my.oschina.net/nandecanghai123/blog/1529339

你可能感兴趣的:(移动开发,java)