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>