viewpager+raidgoud

本文介绍了一个使用ViewPager和RadioGroup实现页面切换联动的示例应用。通过自定义Adapter,实现了不同RadioButton与ViewPager中各个Fragment之间的联动效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.lbs.zuo_yue.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lbs.zuo_yue.R;
import com.lbs.zuo_yue.chilfrags.cf1;
import com.lbs.zuo_yue.chilfrags.cf2;
import com.lbs.zuo_yue.chilfrags.cf3;
import com.lbs.zuo_yue.chilfrags.cf4;

/**
 * author:Created by WangZhiQiang on 2017/11/16.
 */

public class f1 extends Fragment {

    private ViewPager viewPager;
    private RadioGroup chil_rg;
    private View view;

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

        view = inflater.inflate(R.layout.f1_view, container, false);
        viewPager = view.findViewById(R.id.chil_vp);
        chil_rg = view.findViewById(R.id.chil_rg);

        myvpager mvp = new myvpager(getChildFragmentManager());
        viewPager.setAdapter(mvp);
        getchil_rg();
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        RadioButton chil_rb1 = view.findViewById(R.id.chil_rb1);
                        chil_rb1.setChecked(true);
                        break;
                    case 1:
                        RadioButton chil_rb2 = view.findViewById(R.id.chil_rb2);
                        chil_rb2.setChecked(true);
                        break;
                    case 2:
                        RadioButton chil_rb3 = view.findViewById(R.id.chil_rb3);
                        chil_rb3.setChecked(true);
                        break;
                    case 3:
                        RadioButton chil_rb4 = view.findViewById(R.id.chil_rb4);
                        chil_rb4.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        return view;
    }

    class myvpager extends FragmentPagerAdapter {

        public myvpager(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    cf1 cf1 = new cf1();
                    return cf1;
                case 1:
                    cf2 cf2 = new cf2();
                    return cf2;
                case 2:
                    cf3 cf3 = new cf3();
                    return cf3;
                case 3:
                    cf4 cf4 = new cf4();
                    return cf4;
            }
            return null;
        }

        @Override
        public int getCount() {
            return 4;
        }
    }

    public void getchil_rg() {
        chil_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.rb1:
                        viewPager.setCurrentItem(0);
                         break;
                    case R.id.rb2:
                        RadioButton chil_rb2 = view.findViewById(R.id.chil_rb2);
                        chil_rb2.setChecked(true);
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.rb3:
                        RadioButton chil_rb3 = view.findViewById(R.id.chil_rb3);
                        chil_rb3.setChecked(true);
                        viewPager.setCurrentItem(3);
                        break;
                    case R.id.rb4:
                        RadioButton chil_rb4 = view.findViewById(R.id.chil_rb4);
                        chil_rb4.setChecked(true);
                        viewPager.setCurrentItem(4);
                        break;
                }
            }
        });

    }

}








<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v4.view.ViewPager
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/chil_vp"/>
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/chil_rg">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="chil_frag1"
            android:id="@+id/chil_rb1"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="chil_frag2"
            android:layout_weight="1"
            android:id="@+id/chil_rb2"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="chil_frag3"
            android:layout_weight="1"
            android:id="@+id/chil_rb3"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="chil_frag4"
            android:layout_weight="1"
            android:id="@+id/chil_rb4"/>

    </RadioGroup>

</LinearLayout>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值