Android底部导航栏创建——ViewPager + RadioGroup

news/2024/5/17 16:32:51 标签: android, gitee

Android底部导航栏有多种实现方式,本文详解其中的ViewPager + RadioGroup方式的实现步骤。

我们先来看以下看一下最终做出的效果,使大家有个基本概念。

本结构特点:

1,ViewPager部分触摸左右滑动切换页面,RadioGroup部分中的RadioButton随着自己对应的ViewPager页面出现选中时的状态,包括改变背景颜色,

改变文字颜色,改变图片。其他RadioButton则是未被选中时的状态;

2,当用户点击RadioGroup部分中的RadioButton,被点击的RadioButton出现被选中时的颜色,ViewPager界面对应于RadioButton的页面会出现在当前界面。

可以看到,ViewPager和RadioGroup可以双向联动,不是单向传递。

下面我们通过一个实例完全弄懂ViewPager + RadioGroup结构的用法

首先创建出我们界面的布局,上边一个ViewPager,中间 一条分隔线,下边一个RadioGroup

我们在一个Activity的布局中创建如下的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <!--上边为ViewPager-->

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

    <!--中间为一条分割线-->
    <View
        android:background="@color/divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>

    <!--最下边为RadioGroup-->
    <RadioGroup
        android:id="@+id/radioGroup"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!--第一个RadioButton-->
        <RadioButton
            android:id="@+id/button_1"
            android:text="button_1"
            android:button="@null"
            android:textColor="@color/radiobutton_color_selector"
            android:background="@drawable/radiobutton_bg_selector"
            android:gravity="center"
            android:layout_weight="1"
            android:drawableTop="@drawable/radiobutton_pic_selector"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <!--第二个RadioButton-->
        <RadioButton
            android:id="@+id/button_2"
            android:text="button_2"
            android:button="@null"
            android:textColor="@color/radiobutton_color_selector"
            android:background="@drawable/radiobutton_bg_selector"
            android:gravity="center"
            android:layout_weight="1"
            android:drawableTop="@drawable/radiobutton_pic_selector"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <!--第三个RadioButton-->
        <RadioButton
            android:id="@+id/button_3"
            android:text="button_3"
            android:button="@null"
            android:textColor="@color/radiobutton_color_selector"
            android:background="@drawable/radiobutton_bg_selector"
            android:gravity="center"
            android:layout_weight="1"
            android:drawableTop="@drawable/radiobutton_pic_selector"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <!--第四个RadioButton-->
        <RadioButton
            android:id="@+id/button_4"
            android:text="button_4"
            android:button="@null"
            android:textColor="@color/radiobutton_color_selector"
            android:background="@drawable/radiobutton_bg_selector"
            android:gravity="center"
            android:layout_weight="1"
            android:drawableTop="@drawable/radiobutton_pic_selector"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RadioGroup>

</LinearLayout>

 布局中重要属性说明:

 ①ViewPager的android:layout_height属性值为0,android:layout_weight属性值为1。这两个属性值配合使用的意义是:

  在竖直方向上ViewPager占满父控件的剩余空间,也就是占据LinearLayout中除去分隔线和RadioGroup的剩余空间。

  关于android:layout_weight属性的详细用法请参考:android:layout_weight属性的使用方法总结 - chironmy - 博客园                      

 ②RadioButton的android:button属性值为@null。这个属性值的意义是,去除RadioGroup默认自带显示的小圆圈。

 ③RadioButton的android:gravity属性值为center。这个属性值的意义是,使RadioButton的内容(图片和文字)居中。注意,内容默认情况没有居中。

 ④RadioGroup的android:orientation属性值为horizontal。意为,水平布置其中的RadioButton。

 ⑤RadioButton的android:textColor属性值为@color/radiobutton_color_selector,是一个颜色状态选择器。颜色状态选择器就是一个定义在res/color目录

      下的xml文件,color目录需要我们手动创建。颜色状态选择器的代码如下:  

1

2

3

4

5

6

7

8

9

10

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:color="#f00" android:state_checked="true"/>

    <item android:color="#f00" android:state_pressed="true"/>

    <item android:color="#f00" android:state_selected="true"/>

    <!--没被选中时的颜色-->

    <item android:color="#000"/>

</selector>

关于状态选择器的更详细知识,请参考文章http://www.cnblogs.com/baipengzhan/p/6284682.html                       

⑥RadioButton的android:background属性值为@drawable/radiobutton_bg_selector,这一个背景状态选择器,用来改变背景颜色,代码如下:

1

2

3

4

5

6

7

8

9

10

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true" android:drawable="@color/radiobutton_bg_selected"/>

    <item android:state_checked="true" android:drawable="@color/radiobutton_bg_selected"/>

    <item android:state_pressed="true" android:drawable="@color/radiobutton_bg_selected"/>

    <!--未被选中-->

    <item android:drawable="@color/radiobutton_bg_normal" />

</selector>

这个状态选择器是放置在res/drawable目录下的一个普通状态选择器,该选择器的属性android:drawable的属性值不能直接设置颜色,

颜色要封装在values目录下的colors.xml文件中,否则出错。

⑦RadioButton的android:drawableTop属性值为@drawable/radiobutton_pic_selector,是一个普通的状态选择器,用来改变图片,代码如下:

1

2

3

4

5

6

7

8

9

10

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true" android:drawable="@mipmap/ic_selected"/>

    <item android:state_checked="true" android:drawable="@mipmap/ic_selected"/>

    <item android:state_pressed="true" android:drawable="@mipmap/ic_selected"/>

    <!--未被选中-->

    <item android:drawable="@mipmap/ic_launcher"/>

</selector>

该状态选择器同样放置在res/drawable目录下,选择器的属性值android:drawable属性值变为了图片,注意代码写到此处时,系统可能不会提示,

需要手动将该属性值添加进来。

更多关于状态选择器的知识请参考文章http://www.cnblogs.com/baipengzhan/p/6284682.html  

创建出ViewPager页面盛放的Fragment 

我们创建出对应于四个RadioButton的四个Fragment,每个Fragment中盛放一个TextView。下边只列出一个Fragment的写法,剩余的相似,请各位朋友自己写写哦。

public class Fragment_1 extends Fragment {

    private View mView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //注意View对象的重复使用,以便节省资源
        if(mView == null) {
            mView = inflater.inflate(R.layout.fragment_1_layout,container,false);
        }

        return mView;
    }
}

Fragment_1对应的布局:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:gravity="center"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

    <!--创建TextView-->

    <TextView

        android:text="pager_1"

        android:textSize="28sp"

        android:textColor="#00f"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />

</LinearLayout>

在Activity中进行主要逻辑处理

我们在Activity中主要进行的工作如下:

①监听ViewPager,改变RadioGroup中的RadioButton;

②监听RadioGroup中的RadioButton,改变ViewPager;

代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

package com.example.chironmy.bottomnavigation;

import android.os.Bundle;

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.support.v7.app.AppCompatActivity;

import android.view.View;

import android.view.ViewGroup;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,ViewPager.OnPageChangeListener{

    private ViewPager viewPager;

    private RadioGroup radioGroup;

    private RadioButton button_1;

    private RadioButton button_2;

    private RadioButton button_3;

    private RadioButton button_4;

    private Fragment_1 fragment_1;

    private Fragment_2 fragment_2;

    private Fragment_3 fragment_3;

    private Fragment_4 fragment_4;

    private List<Fragment> list;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        //初始化界面

        initView();

    }

    //初始化界面

    private void initView() {

        viewPager = (ViewPager) findViewById(R.id.viewpager);

        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        //找到四个按钮

        button_1 = (RadioButton) findViewById(R.id.button_1);

        button_2 = (RadioButton) findViewById(R.id.button_2);

        button_3 = (RadioButton) findViewById(R.id.button_3);

        button_4 = (RadioButton) findViewById(R.id.button_4);

        //创建Fragment对象及集合

        fragment_1 = new Fragment_1();

        fragment_2 = new Fragment_2();

        fragment_3 = new Fragment_3();

        fragment_4 = new Fragment_4();

        //将Fragment对象添加到list中

        list = new ArrayList<>();

        list.add(fragment_1);

        list.add(fragment_2);

        list.add(fragment_3);

        list.add(fragment_4);

        //给viewPager设置适配器,以显示内容

        MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(adapter);

        //设置RadioGroup开始时设置的按钮,设置第一个按钮为默认值

        radioGroup.check(R.id.button_1);

        //设置Viewpager第一次显示的页面

        viewPager.setCurrentItem(0,true);

        //设置按钮点击监听

        button_1.setOnClickListener(this);

        button_2.setOnClickListener(this);

        button_3.setOnClickListener(this);

        button_4.setOnClickListener(this);

        //设置ViewPager页面监听

        viewPager.addOnPageChangeListener(this);

    }

    @Override

    public void finish() {

        ViewGroup viewGroup = (ViewGroup) getWindow().getDecorView();

        viewGroup.removeAllViews();

        super.finish();

    }

    //创建ViewPager盛放Fragment的适配器类

    public class MyViewPagerAdapter extends FragmentPagerAdapter {

        public MyViewPagerAdapter(FragmentManager fm) {

            super(fm);

        }

        //返回每个position对应的Fragment对象

        @Override

        public Fragment getItem(int position) {

            return list.get(position);

        }

        //返回list的长度,也就是Fragment对象的个数

        @Override

        public int getCount() {

            return list.size();

        }

    }

    //处理点击的方法

    @Override

    public void onClick(View v) {

        //我们根据参数的id区别不同按钮

        //不同按钮对应着不同的ViewPager页面

        switch (v.getId()) {

            case R.id.button_1:

                viewPager.setCurrentItem(0,true);

                break;

            case R.id.button_2:

                viewPager.setCurrentItem(1,true);

                break;

            case R.id.button_3:

                viewPager.setCurrentItem(2,true);

                break;

            case R.id.button_4:

                viewPager.setCurrentItem(3,true);

                break;

            default:

                break;

        }

    }

    //处理页面变化的方法

    @Override

    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    //本文章中我们使用这个方法,本方法处理页面变化后,也就是切换了不同的页面后所做的操作

    @Override

    public void onPageSelected(int position) {

        //根据当前展示的ViewPager页面,使RadioGroup对应的按钮被选中

        switch (position) {

            case 0:

                radioGroup.check(R.id.button_1);

                break;

            case 1:

                radioGroup.check(R.id.button_2);

                break;

            case 2:

                radioGroup.check(R.id.button_3);

                break;

            case 3:

                radioGroup.check(R.id.button_4);

                break;

            default:

                break;

        }

    }

    @Override

    public void onPageScrollStateChanged(int state) {

    }

}

注意:在onClick方法中,viewPager的setCurrentItem方法中的第二个参数的意义是:

            当该参数为true时,viewPager换页时是平滑的换页,会有页面移动的效果;

            该参数为false时,viewPager换页效果没有平滑的移动,页面会直接出现。

            该方法有一个参数的重载方法,默认有平滑换页效果。  

以上代码中很多可以优化,比如xml文件中大量的属性可以提取样式,等等,这里列出只是为了方便更多水平的读者读懂,请谅解。


http://www.niftyadmin.cn/n/5349738.html

相关文章

mac 10.15.7 Unity 2021.3.14 XCode 12.4 -> Unity IOS 自动安装 Cocoapods 失败解决方法

自己这两天在用Unity开发IOS时&#xff0c;遇到了安装Cocoapods失败的问题&#xff0c;记录一下问题及解决方法&#xff0c;便于自己后续查看&#xff0c;以及有相同遭遇的人查看 发生场景&#xff1a;打开 unity&#xff0c;触发自动安装 Cocoapods -> 安装失败&#xff08…

LeetCode 热题 100 | 普通数组

目录 1 53. 最大子数组和 2 56. 合并区间 3 189. 轮转数组 4 238. 除自身以外数组的乘积 5 41. 缺失的第一个正数 菜鸟做题第二周&#xff0c;语言是 C 1 53. 最大子数组和 题眼&#xff1a;“子数组是数组中的一个连续部分。” 遍历数组&#xff0c;问每一个元素…

幻兽帕鲁服务器多少钱一台?腾讯云新版报价

腾讯云幻兽帕鲁服务器4核16G、8核32G和16核64G配置可选&#xff0c;4核16G14M带宽66元一个月、277元3个月&#xff0c;8核32G22M配置115元1个月、345元3个月&#xff0c;16核64G35M配置580元年1个月、1740元3个月、6960元一年&#xff0c;腾讯云百科txybk.com分享腾讯云幻兽帕鲁…

【前端web入门第一天】03 综合案例 个人简介与vue简介

综合案例一 个人简介 网页制作思路:从上到下&#xff0c;先整体再局部&#xff0c;逐步分析制作 分析内容→写代码→保存→刷新浏览器&#xff0c;看效果 个人简介:书写步骤注意几点 1.!回车 生成模板 2.crtlx 剪切之后,空一格,光标前移,写strong或ins之类的,然后回车补全 3.…

华为配置在用户物理位置变化时部署业务随行示例(V200R006C00、V200R007C00、V200R008C00)

配置在用户物理位置变化时部署业务随行示例&#xff08;V200R006C00、V200R007C00、V200R008C00&#xff09; 业务随行简介配置注意事项组网需求需求分析数据规划配置思路操作步骤配置文件 组网图形 图1 组网图 业务随行简介配置注意事项组网需求需求分析数据规划配置思路操作步…

ABAP:ALV 报表

1、可编辑的ALV表格 宏定义 DATA:gt_fieldcat TYPE lvc_t_fcat WITH HEADER LINE.DEFINE fill_alv.CLEAR GT_FIELDCAT.GT_FIELDCAT-FIELDNAME &1. "变量1 字段名GT_FIELDCAT-COLTEXT &2. "变量2 字段描述GT_FIELDCAT-OUTPUTLEN &3. &q…

【ArcGIS遇上Python】python实现批量XY坐标生成shp点数据文件

单个手动生成:【ArcGIS风暴】ArcGIS 10.2导入Excel数据X、Y坐标(经纬度、平面坐标),生成Shapefile点数据图层 文章目录 一、问题分析二、解决办法三、注意事项一、问题分析 现有多个excel、txt或者csv格式的坐标数据,需要根据其坐标批量一键生成shp点数据,如下X为经度,…

【探索科技 感知未来】文心一言大模型

【探索科技 感知未来】文心大模型 &#x1f6a9;本文介绍 文心一言大模型是由中国科技巨头百度公司研发的一款大规模语言模型&#xff0c;其基于先进的深度学习技术和海量数据训练而成。这款大模型具备强大的自然语言处理能力&#xff0c;可以理解并生成自然语言&#xff0c;为…