如何将viewpager与radiogroup关联

如何将viewpager与radiogroup关联,第1张

Android ViewPager与radiogroup实现关联步骤

1.实例化ViewPager

2.通过LayoutInflater加载布局,返回View结果

3.把生成的每一个View对象添加到List集合中

4.实例化适配器,传递View集合

5.在适配器中继承自PagerAdapter,实现内部的四个方法

getCount()返回视图的数量

isViewFromObject()是否通过对象加载视图 View==object

instantiateltem()加载当前页面

(通过container.addView()添加视图)

返回个给用户

destroyItem()销毁滑出的视图

(通过container.removerView()销毁视图)

6.实例化每个RadioButton

7.点击每个RaidoButton时,切换不同的页面

(viewPager.setCurrentltem(下标))

8.当页面切换后,还要把当前的导航栏变为绿色

设置文本颜色的

setTextColor(getResources().getColor(R.color.tvGreen))

设置文本的上方的图片的,四个参数分别为,左、上、右、下

setCompoundDrawablesWithIntrinsicBounds

(null,getResources().getDrawable)

(R.drawable.call_t),null,null)

9.当你每次点击之前的时候,添加一个方法,清除方法,

(清理之 前的所有导航栏的状态,置为灰色)

10.实现滑动监听需要addOnPagerChangeListener

11.在onPagerSelected方法中,根据position页面的下标判断分别设置对应的底部导航栏状态

代码演示

1.在主布局文件中引入android-support-v4.jar包并添加RadioGroup并在RadioGroup中添加RadioButton用于显示导航栏

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

<LinearLayout

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

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="com.example.cxy.viewpager.MainActivity">

以上是Google对于RadioButton和RadioGroup的介绍,主要用于和ViewPager的联合使用来切换fragment的选择的,但是实际开发中已经很少使用了,因为Google退出了更加简便的方式实现RadioGroup和RadioButton组合使用可以达到的效果,后续会总结到的(TabLayout和BottomNavigationView)。

RadioButton是TextView的间接子类,拥有TextView的所有属性,但是在RadioButton的直接父类CompoundButton又加入了几个新的属性:

RadioGroup是LinearLayout的直接子类,拥有LinearLayout的所有属性,同样的为了和RadioButton联合使用,也加入了一个新的属性:

总的来说,这两个控件使用起来还是很简单的,也没有过多的属性,都是一些浅显易懂的,基本上看一遍都可以直接使用起来的。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/bake/7903481.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-11
下一篇2023-04-11

发表评论

登录后才能评论

评论列表(0条)

    保存