//main布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainTabHost"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/main_vp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioGroup
android:id="@+id/RadioGroup_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/RadioButton_home"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/checkimage_home"
android:gravity="center"
android:text="首页"
android:textColor="@drawable/checktext_home" />
<RadioButton
android:id="@+id/RadioButton_class"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/checkimage_class"
android:gravity="center"
android:text="分类"
android:textColor="@drawable/checktext_class" />
<RadioButton
android:id="@+id/RadioButton_car"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1.02"
android:button="@null"
android:drawableTop="@drawable/checkimage_car"
android:gravity="center"
android:text="购物车"
android:textColor="@drawable/checktext_class" />
<RadioButton
android:id="@+id/RadioButton_oneself"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/checkimage_oneself"
android:gravity="center"
android:text="个人"
android:textColor="@drawable/checktext_class" />
</RadioGroup>
</LinearLayout>
//pageradapter 适配器
public class pageradapter extends FragmentPagerAdapter {
public pageradapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
switch (position){
case 0:
fragment=new HomeFragment();
break;
case 1:
fragment=new ClassFragment();
break;
case 2:
fragment=new CarFragment();
break;
case 3:
fragment=new OnselfFragment();
break;
}
return fragment;
}
@Override
public int getCount() {
return 4;
}
}
//MainActivity
public class MainActivity extends AppCompatActivity {
private RadioGroup rg;
private ViewPager vp;
private RadioButton rb_home,rb_class,rb_oneself,rb_car;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化组件
rg = (RadioGroup) findViewById(R.id.RadioGroup_home);
vp = (ViewPager) findViewById(R.id.main_vp);
rb_home = (RadioButton) findViewById(R.id.RadioButton_home);
rb_class = (RadioButton) findViewById(R.id.RadioButton_class);
rb_car = (RadioButton) findViewById(R.id.RadioButton_car);
rb_oneself = (RadioButton) findViewById(R.id.RadioButton_oneself);
//设置适配器
vp.setAdapter(new pageradapter(getSupportFragmentManager()));
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
switch (checkedId){
case R.id.RadioButton_home:
vp.setCurrentItem(0);
break;
case R.id.RadioButton_class:
vp.setCurrentItem(1);
break;
case R.id.RadioButton_car:
vp.setCurrentItem(2);
break;
case R.id.RadioButton_oneself:
vp.setCurrentItem(3);
break;
}
}
});
}
//Fragment
public class CarFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_car,container,false);
return view;
}
}
ViewPager+Fragment滑动
最新推荐文章于 2020-09-06 11:12:10 发布
本文介绍了一个Android应用的主布局设计,通过LinearLayout和ViewPager实现了不同页面间的切换,并使用RadioGroup来作为底部导航栏,包括首页、分类、购物车和个人四个部分。同时,展示了如何通过监听RadioGroup的选择变化来更新ViewPager的当前显示页面。
747

被折叠的 条评论
为什么被折叠?



