RadioGroup+ViewPager+Fragment的组合使用

1. 简介

上一个章节我们说了BottomNavigationView的基本使用,里面也配合了fragment。
连接:https://www.jianshu.com/p/3e73d9b3b459(BottomNavigationView的基本使用)
虽然只是简单的把代码列出来,基本原理是fragment的add的方法添加界面。

RadioGroup+ViewPager也可以做成BottomNavigationView的效果。左右的滑动,可以通过移动点的距离来判断,但是这个没有viewpager来的好。

这一章节介绍一下viewpager的使用list<Fragment> 类型的添加方式。
代码还是使用上一章讲的部分来继续。

下面是显示效果:
GIF.gif

2.布局

<?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">

    <RadioGroup
        android:id="@+id/wallpaper_rg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/recommend"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:gravity="center"
            android:text="@string/recommend"
            android:textColor="#000000"
            android:textSize="20sp" />

        <RadioButton
            android:id="@+id/classification"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:button="@null"
            android:gravity="center"
            android:text="@string/classification"
            android:textColor="@color/wallpaper_title"
            android:textSize="16sp" />

        <RadioButton
            android:id="@+id/local"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:button="@null"
            android:gravity="center"
            android:text="@string/local"
            android:textColor="@color/wallpaper_title"
            android:textSize="16sp" />
    </RadioGroup>

    <android.support.v4.view.ViewPager
        android:id="@+id/wallpaper_vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp">

    </android.support.v4.view.ViewPager>
</LinearLayout>

RadioButton如果只是想要显示文字,android:button可以设置成@null
RadioGroup是一个线性布局的类型,android:orientation可以设置

3.viewpager的适配器

public class WallpaperPaperAdapter extends FragmentPagerAdapter{

    private List<Fragment> mList;
    //传入一个list<Fragment>类型的数组
    public WallpaperPaperAdapter(FragmentManager fm, List<Fragment> list) {
        super(fm);
        this.mList = list;
    }

    //返回数组的长度
    @Override
    public int getCount() {
        return mList.size();
    }

    //得到当前的position
    @Override
    public Fragment getItem(int position) {
        return mList.get(position);
    }
}

因为是通过list<view>的方式来添加,需要加载和销毁页面。所以需要加入instantiateItem方法和destroyItem方法

4.viewpager切换fragment的代码

public class WallpaperFragment extends Fragment implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {

    private View wallpaperView;
    private RadioButton mRecommend;
    private RadioButton mClassification;
    private RadioButton mLocal;
    private RadioGroup mWallpaperRg;
    private ViewPager mWallpaperVp;
    private List<Fragment> mList;

    //3个fragment的定义
    private Fragment_Recommend fragment_recommend;
    private Fragment_Classification fragment_classification;
    private Fragment_Local fragment_local;

    //适配器的定义
    private WallpaperPaperAdapter wallpaperPaperAdapter;
    //定义绘制文字的画笔
    private TextPaint textPaint;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        wallpaperView = inflater.inflate(R.layout.fragment_wallpaper, container, false);
        initData();
        initView();
        return wallpaperView;
    }

    private void initView() {
        mRecommend = (RadioButton) wallpaperView.findViewById(R.id.recommend);
        mClassification = (RadioButton) wallpaperView.findViewById(R.id.classification);
        mLocal = (RadioButton) wallpaperView.findViewById(R.id.local);
        mWallpaperRg = (RadioGroup) wallpaperView.findViewById(R.id.wallpaper_rg);
        mWallpaperVp = (ViewPager) wallpaperView.findViewById(R.id.wallpaper_vp);

        //radiogroup的点击事件
        mWallpaperRg.setOnCheckedChangeListener(this);
        //添加适配器
        mWallpaperVp.setAdapter(wallpaperPaperAdapter);
       //这里没有使用setOnPageChangeListener来设置是因为,setOnPageChangeListener已经过时,显示使用addOnPageChangeListener也是一样的效果,用法不变
        mWallpaperVp.addOnPageChangeListener(this);
    }

    private void initData() {
        mList = new ArrayList<Fragment>();
        //实例化List数组
        fragment_recommend = new Fragment_Recommend();
        fragment_classification = new Fragment_Classification();
        fragment_local = new Fragment_Local();
        //添加到数组中
        mList.add(fragment_recommend);
        mList.add(fragment_classification);
        mList.add(fragment_local);
        //把list传给适配器
        wallpaperPaperAdapter = new WallpaperPaperAdapter(getChildFragmentManager(), mList);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        clearState();
        switch (checkedId) {
            case R.id.recommend:
                Log.d("lcr", "mWallpaperVp 0 ->");
                mWallpaperVp.setCurrentItem(0);
                mRecommend.setTextColor(Color.BLACK);
                mRecommend.setTextSize(20);
                textPaint = mRecommend.getPaint();
                textPaint.setFakeBoldText(true);
                break;
            case R.id.classification:
                Log.d("lcr", "mWallpaperVp 1 ->");
                mWallpaperVp.setCurrentItem(1);
                mClassification.setTextColor(Color.BLACK);
                mClassification.setTextSize(20);
                textPaint = mClassification.getPaint();
                textPaint.setFakeBoldText(true);
                break;
            case R.id.local:
                Log.d("lcr", "mWallpaperVp 2 ->");
                mWallpaperVp.setCurrentItem(2);
                mLocal.setTextColor(Color.BLACK);
                mLocal.setTextSize(20);
                textPaint = mLocal.getPaint();
                textPaint.setFakeBoldText(true);
                break;
            default:
                break;

        }
    }

    //滑动过程中的动作
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    //选择某个页面松手后会被调用
    @Override
    public void onPageSelected(int position) {
        clearState();
        switch (position) {
            case 0:
                //这里必须设置一下setChecked,不然当viewpager滑动到最右边,无法点击最左边的那个radiogroup里面的radiobutton
                mRecommend.setChecked(true);
                mRecommend.setTextColor(Color.BLACK);
                mRecommend.setTextSize(20);
                textPaint = mRecommend.getPaint();
                textPaint.setFakeBoldText(true);
                break;
            case 1:
                mClassification.setChecked(true);
                mClassification.setTextColor(Color.BLACK);
                mClassification.setTextSize(20);
                textPaint = mClassification.getPaint();
                textPaint.setFakeBoldText(true);
                break;
            case 2:
                mLocal.setChecked(true);
                mLocal.setTextColor(Color.BLACK);
                mLocal.setTextSize(20);
                textPaint = mLocal.getPaint();
                textPaint.setFakeBoldText(true);
                break;
            default:
                break;

        }
    }

    //手指放上去,松开,拖动都会被调用
    //state的状态有三个,0表示什么都没做,1正在滑动,2滑动完毕
    @Override
    public void onPageScrollStateChanged(int state) {

    }

    //初始化底部导航栏
    private void clearState() {
        mRecommend.setTextColor(getResources().getColor(R.color.wallpaper_title));
        mClassification.setTextColor(getResources().getColor(R.color.wallpaper_title));
        mLocal.setTextColor(getResources().getColor(R.color.wallpaper_title));

        mRecommend.setTextSize(16);
        mClassification.setTextSize(16);
        mLocal.setTextSize(16);
        //设置文字加粗
        textPaint = mRecommend.getPaint();
        textPaint.setFakeBoldText(false);

        textPaint = mClassification.getPaint();
        textPaint.setFakeBoldText(false);

        textPaint = mLocal.getPaint();
        textPaint.setFakeBoldText(false);
    }
}

功能代码部分,我已经做了一些注释。

注意:这里的Fragment都是导入的v4的包

这里有好一个比较好的介绍,来自 作者:南顾夏浅
链接:https://www.jianshu.com/p/5d06e6faf155

app包下的fragment和v4包下fragment的区别
1.app包中的fragment在3.0以上才可以使用,最好使用兼容低版本的。
2.v4包下的可以兼容到 1.6版本。
3.两者都可以使用<fragment>标签,但是app包下的fragment所在的activity继承Activity即可。v4包下的fragment所在的activity必须继承FragmentActivity,否则会报错。
4.getSupportFragmentManager()对应的是 v4 ;getFragmentManager()对应的是 app;

5.fragment的功能代码和布局

1.布局

为了简洁代码,这里直接写一个背景颜色。

<?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">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#9b1010" />
</LinearLayout>
2.功能代码

只是一个简单的Fragment,其他的页面也可以直接复制

public class Fragment_Classification extends Fragment {

    private View myView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.wallpaper_classification, container, false);
        return myView;

    }
}

6.小结

1. viewpager的适配器是继承FragmentPagerAdapter的
如果继承PagerAdapter那么可以做成一个滚动的导航栏
2.viewpager的点击事件函数设置成addOnPageChangeListener,与原来的setOnPageChangeListener用法一样,但是setOnPageChangeListener已过时
3.viewpager滑动时,需要对radiogroup设置setChecked,不设置可能无法点击

7.Githup的下载地址

https://github.com/githubsmallluo/ViewPager04.git

可以给作者评论,关注加喜欢吗?

QAQ.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容