viewpager+fragment+radiogroup横向滑动切换界面

package com.bwie.zhangpengfei20180813.view.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioGroup;

import com.bwie.zhangpengfei20180813.R;
import com.bwie.zhangpengfei20180813.view.fragment.Fragment01;
import com.bwie.zhangpengfei20180813.view.fragment.Fragment02;
import com.bwie.zhangpengfei20180813.view.fragment.Fragment03;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.rg)
    RadioGroup rg;
    private ArrayList list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        list = new ArrayList<>();
        Fragment01 fragment01 = new Fragment01();
        Fragment02 fragment02 = new Fragment02();
        Fragment03 fragment03 = new Fragment03();

        list.add(fragment01);
        list.add(fragment02);
        list.add(fragment03);

        getSupportFragmentManager().beginTransaction().replace(R.id.vp,new Fragment01()).commit();

        FragmentManager fragmentManager = getSupportFragmentManager();

        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()){

            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }
        });

        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb01:
                        vp.setCurrentItem(0);
                        break;
                    case R.id.rb02:
                        vp.setCurrentItem(1);
                        break;
                    case R.id.rb03:
                        vp.setCurrentItem(2);
                        break;

                }
            }
        });

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        rg.check(R.id.rb01);
                        break;
                    case 1:
                        rg.check(R.id.rb02);
                        break;
                    case 2:
                        rg.check(R.id.rb03);
                        break;

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}

你可能感兴趣的:(Android)