滑动或点击按钮切换页面 ViewPager+RadioGroup+Fragment

举例:男士购App

首页图片(选中和未选中):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/ic_nav_home_press" android:state_checked="true"></item>
    <item android:drawable="@mipmap/ic_nav_home" android:state_checked="false"></item>
    <item android:drawable="@mipmap/ic_nav_home"></item>
</selector>
分类图片(选中和未选中):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/ic_nav_class_press" android:state_checked="true"></item>
    <item android:drawable="@mipmap/ic_nav_class" android:state_checked="false"></item>
    <item android:drawable="@mipmap/ic_nav_class"></item>
</selector>
购物车图片(选中和未选中):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/ic_nav_cart_press" android:state_checked="true"></item>
    <item android:drawable="@mipmap/ic_nav_cart" android:state_checked="false"></item>
    <item android:drawable="@mipmap/ic_nav_cart"></item>
</selector>

个人图片(选中和未选中):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/ic_nav_user_press" android:state_checked="true"></item>
    <item android:drawable="@mipmap/ic_nav_user" android:state_checked="false"></item>
    <item android:drawable="@mipmap/ic_nav_user"></item>
</selector>

按钮文字选中和未选中状态:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/main_title" android:state_checked="true"></item>
    <item android:color="@color/main_text" android:state_checked="false"></item>
</selector>

activity_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="40dp"
            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="40dp"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/checkimage_class"
            android:gravity="center"
            android:text="分类"
            android:textColor="@drawable/checktext_home" />

        <RadioButton
            android:id="@+id/RadioButton_car"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_weight="1.02"
            android:button="@null"
            android:drawableTop="@drawable/checkimage_car"
            android:gravity="center"
            android:text="购物车"
            android:textColor="@drawable/checktext_home" />

        <RadioButton            
            android:id="@+id/RadioButton_oneself" 
            android:layout_width="wrap_content"
           android:layout_height="40dp"
          android:layout_weight="1"
          android:button="@null"
          android:drawableTop="@drawable/checkimage_oneself" 
           android:gravity="center"            
          android:text="個人"            
          android:textColor="@drawable/checktext_home" />
  </RadioGroup>
</LinearLayout>
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 MainFragmentPagerAdapter(getSupportFragmentManager()));

        //切换vp(ViewPager页面监听 使用add而不是set)
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (position){
                    case 0:
                        rb_home.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.main));
                        rb_home.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_home_press), null, null);
                        rb_class.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.nav));
                        rb_class.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_class), null, null);
                        rb_car.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.nav));
                        rb_car.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_cart), null, null);
                        rb_oneself.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.nav));
                        rb_oneself.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_user), null, null);
                        break;
                    case 1:
                        rb_home.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.nav));
                        rb_home.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_home), null, null);
                        rb_class.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.main));
                        rb_class.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_class_press), null, null);
                        rb_car.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.nav));
                        rb_car.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_cart), null, null);
                        rb_oneself.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.nav));
                        rb_oneself.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_user), null, null);
                        break;
                    case 2:
                        rb_home.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.nav));
                        rb_home.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_home), null, null);
                        rb_class.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.nav));
                        rb_class.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_class), null, null);
                        rb_car.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.main));
                        rb_car.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_cart_press), null, null);
                        rb_oneself.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.nav));
                        rb_oneself.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_user), null, null);
                        break;
                    case 3:
                        rb_home.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.nav));
                        rb_home.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_home), null, null);
                        rb_class.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.nav));
                        rb_class.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_class), null, null);
                        rb_car.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.nav));
                        rb_car.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_cart), null, null);
                        rb_oneself.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.main));
                        rb_oneself.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(MainActivity.this, R.mipmap.ic_nav_user_press), null, null);
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                /*/0是静止,1是正在滑动,2是停止滑动
                if (state == 2) {
                    //设置滑动ViewPager导航同步变化
                    myBottomLayout.setResidAndColor(vp.getCurrentItem());
                }*/
            }
        });

        //点击RadioButton
        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;

                }
            }
        });

    }
}

适配器FragmentPagerAdapter 代码:
public class MainFragmentPagerAdapter extends FragmentPagerAdapter {

    public MainFragmentPagerAdapter(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;
    }
}

Fragment代码:
public class HomeFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home,container,false);


        return view;
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值