ViewPager+Fragment滑动

本文介绍了一个Android应用的主布局设计,通过LinearLayout和ViewPager实现了不同页面间的切换,并使用RadioGroup来作为底部导航栏,包括首页、分类、购物车和个人四个部分。同时,展示了如何通过监听RadioGroup的选择变化来更新ViewPager的当前显示页面。
//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;
    }
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值