fragment和radiobutton

本文介绍如何使用 Android 中的 ViewPager 控件结合 Fragment 实现页面切换效果,并通过 RadioButton 控制页面显示,实现流畅的导航体验。

MainActivty

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private ViewPager viewPager;
    private ArrayList<Fragment> fragments;
    FragmentOne one;
    FragementTwo two;
    FragmentThree three;
    FragmentFour four;
    FragmentManager fm;
    private RadioButton b1,b2,b3,b4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager= (ViewPager) findViewById(R.id.vp);//初始化控件
        fm=getSupportFragmentManager();//得到管理器
        fragments=new ArrayList<Fragment>();//添加集合
         one = new FragmentOne();
        two = new FragementTwo();
         three = new FragmentThree();//实例化fragment
         four = new FragmentFour();
        fragments.add(one);//添加frgmen
        fragments.add(two);
        fragments.add(three);
        fragments.add(four);
        viewPager.setAdapter(new FragmentPagerAdapter(fm) {//给viewpager添加适配器
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });
        b1= (RadioButton) findViewById(R.id.manhua);
        b2= (RadioButton) findViewById(R.id.faxian);
        b3= (RadioButton) findViewById(R.id.shequ);
        b4= (RadioButton) findViewById(R.id.wode);
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
        b4.setOnClickListener(this);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {//设置viewpager的监听
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                    switch (position)
                    {
                        case 0:
                            b1.setBackgroundColor(Color.YELLOW);
                            b2.setBackgroundColor(Color.GRAY);
                            b3.setBackgroundColor(Color.GRAY);
                            b4.setBackgroundColor(Color.GRAY);
                            break;
                        case 1:
                            b1.setBackgroundColor(Color.GRAY);
                            b2.setBackgroundColor(Color.YELLOW);
                            b3.setBackgroundColor(Color.GRAY);
                            b4.setBackgroundColor(Color.GRAY);
                            break;
                        case 2:
                            b1.setBackgroundColor(Color.GRAY);
                            b2.setBackgroundColor(Color.GRAY);
                            b3.setBackgroundColor(Color.YELLOW);
                            b4.setBackgroundColor(Color.GRAY);
                            break;
                        case 3:
                            b1.setBackgroundColor(Color.GRAY);
                            b2.setBackgroundColor(Color.GRAY);
                            b3.setBackgroundColor(Color.GRAY);
                            b4.setBackgroundColor(Color.YELLOW);
                            break;
                    }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.manhua:
                viewPager.setCurrentItem(0);
                b1.setBackgroundColor(Color.YELLOW);
                b2.setBackgroundColor(Color.GRAY);
                b3.setBackgroundColor(Color.GRAY);
                b4.setBackgroundColor(Color.GRAY);
                break;
            case R.id.faxian:
                viewPager.setCurrentItem(1);
                b1.setBackgroundColor(Color.GRAY);
                b2.setBackgroundColor(Color.YELLOW);
                b3.setBackgroundColor(Color.GRAY);
                b4.setBackgroundColor(Color.GRAY);
                break;
            case R.id.shequ:
                viewPager.setCurrentItem(2);
                b1.setBackgroundColor(Color.GRAY);
                b2.setBackgroundColor(Color.GRAY);
                b3.setBackgroundColor(Color.YELLOW);
                b4.setBackgroundColor(Color.GRAY);
                break;
            case R.id.wode:
                viewPager.setCurrentItem(3);
                b1.setBackgroundColor(Color.GRAY);
                b2.setBackgroundColor(Color.GRAY);
                b3.setBackgroundColor(Color.GRAY);
                b4.setBackgroundColor(Color.YELLOW);
                break;
        }
    }
}


xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    >

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <RadioGroup
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <RadioButton
            android:id="@+id/manhua"
            android:text="漫画"
            android:button="@null"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
        <RadioButton
            android:id="@+id/faxian"
            android:text="发现"
            android:button="@null"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
        <RadioButton
            android:id="@+id/shequ"
            android:text="V社区"
            android:button="@null"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
        <RadioButton
            android:id="@+id/wode"
            android:text="我的"
            android:button="@null"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </RadioGroup>
</RelativeLayout>

fragment中

public class FragementTwo extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_two,null);
        return view;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值