ViewPager+Fragment滑动

来源:互联网 发布:阿里云代理加盟 编辑:程序博客网 时间:2024/05/21 17:39
//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;    }}//MainActivitypublic 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;                }            }        });    }//Fragmentpublic 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;    }}