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<Fragment> 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();
    }
}

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#00f"></android.support.v4.view.ViewPager>

    <RadioGroup
        android:id="@+id/radio_group_bottom"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#0f0"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radio_button_home"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:textSize="25sp"
            android:text="首页"
            android:textColor="#000"></RadioButton>

        <RadioButton
            android:id="@+id/radio_button_shop"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:autofillHints="null"
            android:button="@null"
            android:gravity="center"
            android:text="购物车"
            android:textSize="25sp"
            android:textColor="#000"></RadioButton>

        <RadioButton
            android:id="@+id/radio_button_mine"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:autofillHints="null"
            android:button="@null"
            android:gravity="center"
            android:text="我的"
            android:textSize="25sp"
            android:textColor="#000"></RadioButton>
    </RadioGroup>

</LinearLayout>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值