Android之ViewPager与Fragment

功能是利用viewpager,通过按钮 btn1 和按钮 btn2 切换fragment1页面和fragment2页面。

下面是activity_main.xml布局文件:(底部两个按钮,上面一个ViewPager)




    

        

 用ViewPager需要一个adapter来绑定数据,因为ViewPager常常和Fragment组合使用,所以有专门的FragmentPagerAdapter可供使用。

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private int PAGER_COUNT = 2;
    private Fragment1 fragment1 = null;
    private Fragment2 fragment2 = null;

    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = null;
        switch (i) {
            case MainActivity.PAGE_ONE:
                fragment = fragment1;
                break;
            case MainActivity.PAGE_TWO:
                fragment = fragment2;
                break;

        }
        return fragment;
    }

    @Override
    public int getCount() {
        return PAGER_COUNT;
    }

MainActivity类,必须实现Fragment1.OnFragmentInteractionListener、Fragment2.OnFragmentInteractionListener、ViewPager.OnPageChangeListener这三个接口

public class MainActivity extends AppCompatActivity implements Fragment1.OnFragmentInteractionListener, Fragment2.OnFragmentInteractionListener,ViewPager.OnPageChangeListener {

    private String TAG = "MainActivity";
    //Fragment
    public static final int PAGE_ONE = 0;
    public static final int PAGE_TWO = 1;
    private ViewPager vpager;
    private MyFragmentPagerAdapter mAdapter;
    Button btn1;
    Button btn2;

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

        //ViewPager//fragment
        mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        vpager = findViewById(R.id.view_pager);
        vpager.setAdapter(mAdapter);
        vpager.setCurrentItem(PAGE_ONE);
        vpager.addOnPageChangeListener(this);

      
    }

    private void initView(){
        btn1 = findViewById(R.id.btn_1_page);
        btn2 = findViewById(R.id.btn_2_page);
        //设置drawableTop
        btn1.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.folder_36dp, 0, 0);
        btn1.setTextColor(getResources().getColor(R.color.mainColor));
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setPageOne();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setPageTwo();
            }
        });
    }

    private void setPageOne(){
        vpager.setCurrentItem(PAGE_ONE);
        btn1.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.img, 0, 0);
        btn1.setTextColor(getResources().getColor(R.color.mainColor));
        btn2.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.img_grey, 0, 0);
        btn2.setTextColor(getResources().getColor(R.color.gray));
    }

    private void setPageTwo(){
        vpager.setCurrentItem(PAGE_TWO);
        btn2.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.img, 0, 0);
        btn2.setTextColor(getResources().getColor(R.color.mainColor));
        btn1.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.img_grey, 0, 0);
        btn1.setTextColor(getResources().getColor(R.color.gray));
    }

    
    
    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        Log.i(TAG,"PAGE"+i);
        switch (i){
            case 0:
                setPageOne();
                break;
            case 1:
                setPageTwo();
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}

 

你可能感兴趣的:(android,开发笔记,android之界面,Android,ViewPager,Fragment)