RadioGroup与Viewpager联动

1、布局文件


<RelativeLayout
    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" tools:context="com.example.app2.MainActivity">


    <RadioGroup
        android:id="@+id/rg"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:layout_width="0dp"
            android:text="首页"
            android:id="@+id/shouye"
            android:background="@drawable/selector"
            android:checked="true"
            android:gravity="center_horizontal"
            android:textSize="20sp"
            android:button="@null"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <RadioButton
            android:layout_width="0dp"
            android:text="资讯"
            android:id="@+id/zixun"
            android:background="@drawable/selector"
            android:gravity="center_horizontal"
            android:textSize="20sp"
            android:button="@null"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <RadioButton
            android:layout_width="0dp"
            android:text="发现"
            android:id="@+id/faxian"
            android:background="@drawable/selector"
            android:gravity="center_horizontal"
            android:textSize="20sp"
            android:button="@null"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <RadioButton
            android:layout_width="0dp"
            android:text="我的"
            android:id="@+id/wode"
            android:background="@drawable/selector"
            android:gravity="center_horizontal"
            android:textSize="20sp"
            android:button="@null"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

    RadioGroup>
    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_above="@id/rg"
        android:layout_width="match_parent"
        android:layout_height="match_parent">android.support.v4.view.ViewPager>
RelativeLayout>

2、MainActivity的书写

package com.example.app2;

import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RadioGroup rg;
    private ViewPager vp;
    private List list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局
        setContentView(R.layout.activity_main);
        //添加集合
        list = new ArrayList<>();
        //添加数据
        list.add(new Fragment1());
        list.add(new Fragment2());
        list.add(new Fragment3());
        list.add(new Fragment4());
        //找控件
        rg = (RadioGroup) findViewById(R.id.rg);
        vp = (ViewPager) findViewById(R.id.vp);

        //viewpaer一次加载全部页面
        vp.setOffscreenPageLimit(list.size());

        //设置适配器
        vp.setAdapter(new MyAdapter(getSupportFragmentManager()));
        //RadioGroup监听事件
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.shouye:
                        vp.setCurrentItem(0);
                        break;
                    case R.id.zixun:
                        vp.setCurrentItem(1);
                        break;
                    case R.id.faxian:
                        vp.setCurrentItem(2);
                        break;
                    case R.id.wode:
                        vp.setCurrentItem(3);
                        break;
                }
            }
        });

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

            }

            @Override
            public void onPageSelected(int position) {
                //实现滑动页面下方按钮的联动
                rg.check(rg.getChildAt(position).getId());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    //适配器
    class MyAdapter extends FragmentPagerAdapter{

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


        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

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


    }

}

//Tablayout的切换

package com.example.app2;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
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 java.util.ArrayList;
import java.util.List;

/**
 * Created by 墨鸦 on 2017/10/17.
 */

public class Fragment1 extends Fragment{

    private TabLayout tablayout;
    private ViewPager viewpager;
    private List datas=new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //加载布局
        View view=View.inflate(getActivity(),R.layout.layout1,null);
        //找控件
        tablayout = (TabLayout) view.findViewById(R.id.tablayout);
        viewpager = (ViewPager) view.findViewById(R.id.viewpager);
        //添加数据
        datas.add("推荐");
        datas.add("要闻");
        datas.add("娱乐");
        datas.add("科技");
        datas.add("汽车");
        datas.add("体育");
        datas.add("图片");
        datas.add("动漫");
        datas.add("社会");
        datas.add("游戏");


        viewpager.setOffscreenPageLimit(datas.size());

        //添加适配器
        viewpager.setAdapter(new MyAdapter(getActivity().getSupportFragmentManager()));

        //进行关联

        tablayout.setupWithViewPager(viewpager);
        //返回视图
        return view;
    }

    class  MyAdapter extends FragmentPagerAdapter {

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

        //返回选项卡
        public CharSequence getPageTitle(int position) {
            return datas.get(position);
        }

        @Override
        public Fragment getItem(int position) {//创建fragment并返回

            Fragment f=null;
            switch (position){
                case 0:
                    f=new Fragment_shouye();
                    break;
                default:
                    Bundle bun=new Bundle();
                    bun.putString("key",datas.get(position).toString());

                    Fram fr=new Fram();
                    fr.setArguments(bun);
                    return fr;
            }
            return f;




        }

        @Override
        public int getCount() {//返回选项卡数量
            return datas.size();
        }
    }

}

Bundle传值

//传值
Bundle bun=new Bundle();
bun.putString("key",datas.get(position).toString());
Fragment fr=new Fragment();
fr.setArguments(bun);


//接收
Bundle  bun=getArguments();
bun.getString("key");

你可能感兴趣的:(RadioGroup与Viewpager联动)