ViewPager+RadioGroup实现标题栏切换,Fragment切换
1.说明:
在使用RadioGroup做标题栏切换的时候,跟ViewPager的滑动有冲突,最后查看了源代码+断点调试攻克了一些碰到的问题,写一篇博客总结一下,有相同需求的朋友能够借鉴一下,自己以后实用到也方便复习。
2.代码结构,以及功能说明
1).主界面的Fragment切换使用ViewPager实现
2).标题栏用RadioGroup实现
3).实现这两个控件的监听函数,改变背景,改变字体颜色,设置当前Fragment,设置当前选中RadioButton
3.主界面代码实现
public class MainActivity extends FragmentActivity {private RadioButton homeFollow,homeRecommend,homeLocation;private ViewPager vPager;private List<Fragment> list=new ArrayList<Fragment>();private MyFragmentAdapter adapter;private final int[] array=new int[]{R.id.home_follow,R.id.home_recommend,R.id.home_location};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.view_pager_test);FollowFragment topFragment = new FollowFragment();RecommendFragment hotFragment = new RecommendFragment();LocationFragment locationFragment = new LocationFragment();list.add(topFragment);list.add(hotFragment);list.add(locationFragment);vPager = (ViewPager) findViewById(R.id.viewpager_home);adapter = new MyFragmentAdapter(getSupportFragmentManager(), list);vPager.setAdapter(adapter);vPager.setOffscreenPageLimit(2);vPager.setCurrentItem(1);vPager.setOnPageChangeListener(pageChangeListener);homeFollow=(RadioButton) findViewById(R.id.home_follow);homeRecommend=(RadioButton) findViewById(R.id.home_recommend);homeLocation=(RadioButton) findViewById(R.id.home_location);RadioGroup group=(RadioGroup) findViewById(R.id.home_page_select);group.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group,int checkedId){//设置了ViewPager的当前item就会触发ViewPager的SimpleOnPageChangeListener监听函数switch (checkedId){case R.id.home_follow:vPager.setCurrentItem(0);break;case R.id.home_recommend:vPager.setCurrentItem(1);break;case R.id.home_location:vPager.setCurrentItem(2);break;}}});}SimpleOnPageChangeListener pageChangeListener=new SimpleOnPageChangeListener(){public void onPageSelected(int position){change(array[position]);}};/*** 改变背景颜色,背景图片* @param checkedId*/private void change(int checkedId){//改变背景颜色homeFollow.setBackgroundResource(R.drawable.icon_top_normal);homeRecommend.setBackgroundResource(R.drawable.icon_recommend_normal);homeLocation.setBackgroundResource(R.drawable.icon_location_normal);//改变字体颜色homeFollow.setTextColor(getResources().getColor(R.color.white_normal));homeRecommend.setTextColor(getResources().getColor(R.color.white_normal));homeLocation.setTextColor(getResources().getColor(R.color.white_normal));switch (checkedId){case R.id.home_follow:homeFollow.setBackgroundResource(R.drawable.icon_top_select);homeFollow.setTextColor(getResources().getColor(R.color.balck_normal));homeFollow.setChecked(true);break;case R.id.home_recommend:homeRecommend.setBackgroundResource(R.drawable.icon_recommend_select);homeRecommend.setTextColor(getResources().getColor(R.color.balck_normal));homeRecommend.setChecked(true);break;case R.id.home_location:homeLocation.setBackgroundResource(R.drawable.icon_location_select);homeLocation.setTextColor(getResources().getColor(R.color.balck_normal));homeLocation.setChecked(true);break;}}
}
4.ViewPager适配器
public class MyFragmentAdapter extends FragmentStatePagerAdapter {private List<Fragment>list;public MyFragmentAdapter(FragmentManager fm, List<Fragment> list) {super(fm);this.list = list;}public MyFragmentAdapter(FragmentManager fm) {super(fm);}@Overridepublic Fragment getItem(int arg0) {return list.get(arg0);}@Overridepublic int getCount() {return list.size();}
}
5.主界面布局文件
<span style="font-size:14px;"><FrameLayout 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.ViewPagerandroid:id="@+id/viewpager_home"android:layout_width="match_parent"android:layout_height="match_parent" /><RelativeLayoutandroid:id="@+id/login_success_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#78000000"android:paddingLeft="5dip"android:paddingRight="5dip" ><RadioGroupandroid:id="@+id/home_page_select"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:orientation="horizontal"android:paddingBottom="4dp"android:paddingTop="4dp" ><RadioButtonandroid:id="@+id/home_follow"android:background="@drawable/icon_top_normal"android:button="@null"android:gravity="center"android:text="关注"android:textColor="@color/select_home_radio_color" /><RadioButtonandroid:id="@+id/home_recommend"android:background="@drawable/icon_recommend_select"android:button="@null"android:checked="true"android:gravity="center"android:text="推荐"android:textColor="@color/select_home_radio_color" /><RadioButtonandroid:id="@+id/home_location"android:background="@drawable/icon_location_normal"android:button="@null"android:gravity="center"android:text="位置"android:textColor="@color/select_home_radio_color" /></RadioGroup></RelativeLayout></FrameLayout></span>
6.效果图例如以下:
另一些布局文件,跟资源文件我就不贴出来了,有须要的能够直接下载源代码
点击下载源代码
推荐下自己创建的android QQ群:202928390 欢迎大家的增加.
推荐一个Android开发人员必关注公众号,每周都有原创干货
ViewPager+RadioGroup实现标题栏切换,Fragment切换相关推荐
- android 中radiogroup滑动切换,巧妙实现缺角radiogroup控制多个fragment切换和滑动
在android开发中,用一个radiogroup控制多个fragment切换是十分常见的需求.但是如果fragment是一个ListView,如何保证滑动的时候通过缺角可以看到下面的listview ...
- ViewPager -- Fragment 切换卡顿 性能优化
当ViewPager切换到当前的Fragment时,Fragment会加载布局并显示内容,如果用户这时快速切换ViewPager,即 Fragment需要加载UI内容,而又频繁地切换Fragment, ...
- ViewPager切换Fragment生命周期变化
很久没有写博客了,今天学习了MD的TabLayout ,使用到了ViewPager,于是对于很久以来的一个疑问:ViewPager切换Fragment生命周期是如何变化的进行了一个测试. 本文研究了: ...
- 【Android】保存Fragment切换状态
[Android]保存Fragment切换状态 前言 一般频繁切换Fragment会导致频繁的释放和创建,如果Fragment比较臃肿体验就非常不好了,这里分享一个方法. 声明 欢迎转载,但请保留文章 ...
- 购物车/分类 /Fragment切换 全
0,Fragment切换 (1)MainActivity public class MainActivity extends AppCompatActivity {private ViewPager ...
- Fragment 使用 show 和 hide 的方式实现切换 以及切换的时候Fragment 生命周期
实现的效果如下图 主要的代码在activity 这里贴出来了 public class ShowActvity extends AppCompatActivity implements View.On ...
- (仿头条APP项目)2.主界面按钮切换Fragment页面
文章目录 主界面按钮切换Fragment页面 效果展示 框架结构 xml视图 主视图 选择器和style java代码模块 创建4个Fragment 1.绑定按钮选择事件 2.初始化和用HashMap ...
- android tab 切换动画,Android之ViewPager+TabLayout组合实现导航条切换效果(微信和QQ底部多标签切换)...
前言 之前在另外一篇中用Fragment和button实现了点击切换Fragment的效果,比较简陋.这次改用ViewPager+TabLayout 实现联动的效果. 实现效果 ViewPager 多 ...
- android fragment实例化,Android使得Fragment 切换时不重新实例化
以前实现Fragment的切换都是用replace方法实现 public void startFragmentAdd(Fragment fragment) { FragmentManager frag ...
最新文章
- 谈“云”色变?近80%企业曾遭受数据泄露
- nginx 负载 sessionid 不一致_你不知道的Nginx负载均衡算法,快进来迅速掌握
- 关于DateTime对象序列化为Json之后的若干问题
- Spring Bean的生命周期(详细)
- 科大星云诗社动态20201211
- JVM 学习四:类加载之双亲委派机制与沙箱安全机制
- 容器编排技术 -- 安装和设置kubectl
- pytorch测试模型时根据不同列别的概率值得到具体的分类
- iOS开发项目篇—02添加子控制器以及项目分层
- 2022年APP软件游戏应用网站Pbootcms模板源码+支持WAP
- C语言基本数据类型详解
- ASA防火墙配置清除
- 为什么五笔输入法打字那么快,现在使用的人却越来越少了?
- MyBatis返回Map时值为null的字段会丢失
- (筆記) 如何在字串中從指定字元抓到指定字元? (C/C++) (C)
- Flutter高仿微信-第26篇-新的朋友
- h5 版活体检测、视频活体检测
- 亲测这款读书笔记app软件能提高你的阅读兴趣
- 项目--静态资源cdn的引入
- java bufferedread_java中关于bufferedreader类中read方法
热门文章
- debug打印的有意思的方法
- matlab多边形扫描线填充算法代码,计算机图形学—多边形扫描与填充
- 查车的行驶轨迹_怎么查车辆行驶轨迹?
- vue list添加元素_Vue简单入门及组件的简单使用
- cfa三级真题和mock_两年高分过CFA三级的女学霸:复习秘诀刷爆了朋友圈!
- matlab 求圆的周长和面积
- 硬件数据手册中如何表示信号高低电平有效?
- 【 FPGA 】序列检测器的Mealy状态机实现
- 【 C 】KR C 与 ANSI C的区别(KR C,ANSI C,C89,C90,C99)(C11)
- 使用ISE创建IP核(以加法器的IP核建立为例)