使用RadioButton做一个ViewPager的指示器

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"></androidx.viewpager.widget.ViewPager>

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>

</LinearLayout>

首先就是布局文件,一个ViewPager和RadioGroup,RadioGroup中放若干个RadioButton,然后在MainActivity中绑定控件,实现ViewPager的适配器,这里我用FragmentPagerAdapter,就先创一个Fragment
在这里插入图片描述
在Fragment中返回一个随机背景色的view,就不用新建太多Fragment来区分

public class BlankFragment extends Fragment {
    View view;

    public BlankFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_blank, container, false);
        Random random = new Random();
        view.setBackgroundColor(Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
        return view;
    }
}

在MainActivity中直接实现vp的适配器,并添加数据

		RadioButton radioButton; 
		List<Fragment> fragments = new ArrayList<>();
		==================================================
		vp = findViewById(R.id.vp);
        rg = findViewById(R.id.rg);
        radioButton= (RadioButton) rg.getChildAt(0);
        radioButton.setChecked(true);
		for (int i = 0; i < 8; i++) {
            fragments.add(new BlankFragment());
        }
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager(), 1) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//                super.destroyItem(container, position, object);
            }
        });

添加ViewPager的监听事件,实现滑动时更改RadioButton的选中

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                radioButton = (RadioButton) rg.getChildAt(position);
                radioButton.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

看看目前的效果图(GIF图片)
在这里插入图片描述
然后添加RadioButton点击切换Fragment,我们使用RadioGroup的监听事件来实现,这一部也很简单,可以说一行代码就实现了

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                **vp.setCurrentItem(i - 1);**
                //这里有BUG,改成下面这句
                **vp.setCurrentItem((i - 1)%fragments.size);**
            }
        });

到此,基本上就实现了我们所需要的功能,同时自己还可以设置RadioButton的样式

最后的成品图如下
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值