Android ViewPager+Fragment+RadioGroup

 Android ViewPager+Fragment+RadioGroup_第1张图片

1.MainActivity.XML




    


    

        

        

        

    



2.Selector结合Shape




    
        
            
            
        
    
    
        
            
            
        
    

3.MainAc

public class MainActivity extends AppCompatActivity {


    private ViewPager view_pager;
    private RadioGroup rgp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        final List fragmentLists = new ArrayList<>();
        BlankFragment blankFragment = new BlankFragment();
        BlankFragment2 blankFragment2 = new BlankFragment2();
        BlankFragment3 blankFragment3 = new BlankFragment3();
        fragmentLists.add(blankFragment);
        fragmentLists.add(blankFragment2);
        fragmentLists.add(blankFragment3);

        view_pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragmentLists.get(i);
            }

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

        view_pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                switch (i) {
                    case 0:
                        rgp.check(R.id.radio1);
                        break;
                    case 1:
                        rgp.check(R.id.radio2);
                        break;
                    case 2:
                        rgp.check(R.id.radio3);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });


        rgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId) {
                    case R.id.radio1:
                        view_pager.setCurrentItem(0);
                        break;
                    case R.id.radio2:
                        view_pager.setCurrentItem(1);
                        break;
                    case R.id.radio3:
                        view_pager.setCurrentItem(2);
                        break;
                    default:
                        break;
                }
            }
        });

        view_pager.setCurrentItem(0);
        rgp.check(R.id.radio1);
    }


    private void initView() {
        view_pager = (ViewPager) findViewById(R.id.view_pager);
        rgp = (RadioGroup) findViewById(R.id.rgp);
    }
}

可以做一个无线翻页+Pagetransform

你可能感兴趣的:(学习ing)