fragment+Viewpager实现布局加载

fragment采用的是动态加载方式,底部采用radiogroup

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.radio_button_home)
    RadioButton radioButtonHome;
    @BindView(R.id.radio_button_shop)
    RadioButton radioButtonShop;
    @BindView(R.id.radio_button_mine)
    RadioButton radioButtonMine;
    @BindView(R.id.radio_group_bottom)
    RadioGroup radioGroupBottom;
    private Unbinder bind;
    private List list;
    private HomeFragment homeFragment;
    private ShoppingFragment shoppingFragment;
    private MineFragment mineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        bind = ButterKnife.bind(this);
        initView();//布局的加载
//        initData();
    }


    private void initView() {
        list = new ArrayList<>();
        homeFragment = new HomeFragment();
        shoppingFragment = new ShoppingFragment();
        mineFragment = new MineFragment();
        list.add(homeFragment);
        list.add(shoppingFragment);
        list.add(mineFragment);
        //viewpager懒加载
//        vp.
        //viewpager适配
        radioButtonHome.setBackgroundColor(Color.RED);
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return list.get(i);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                switch (i) {
                    case 0:
                        radioButtonHome.setBackgroundColor(Color.RED);
                        radioButtonShop.setBackgroundColor(Color.GREEN);
                        radioButtonMine.setBackgroundColor(Color.GREEN);
                        break;
                    case 1:
                        radioButtonHome.setBackgroundColor(Color.GREEN);
                        radioButtonShop.setBackgroundColor(Color.RED);
                        radioButtonMine.setBackgroundColor(Color.GREEN);
                        break;
                    case 2:
                        radioButtonHome.setBackgroundColor(Color.GREEN);
                        radioButtonShop.setBackgroundColor(Color.GREEN);
                        radioButtonMine.setBackgroundColor(Color.RED);
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @OnClick({R.id.radio_button_home, R.id.radio_button_shop, R.id.radio_button_mine})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.radio_button_home:
                vp.setCurrentItem(0);
                break;
            case R.id.radio_button_shop:
                vp.setCurrentItem(1);
                break;
            case R.id.radio_button_mine:
                vp.setCurrentItem(2);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //ButterKnife解绑
        bind.unbind();
    }
}

布局



    

    

        

        

        
    


你可能感兴趣的:(android)