ViewPager+RadioGroup实现标题栏切换,Fragment切换

简介:

1.说明:

在使用RadioGroup做标题栏切换的时候,跟ViewPager的滑动有冲突,最后查看了源码+断点调试解决了一些碰到的问题,写一篇博客总结一下,有同样需求的朋友可以借鉴一下,自己以后有用到也方便复习。


2.代码结构,以及功能说明

    1).主界面的Fragment切换使用ViewPager实现

    2).标题栏用RadioGroup实现

    3).实现这两个控件的监听函数,改变背景,改变字体颜色,设置当前Fragment,设置当前选中RadioButton


3.主界面代码实现

  1. public class MainActivity extends FragmentActivity {  
  2.     private RadioButton homeFollow,homeRecommend,homeLocation;  
  3.     private ViewPager  vPager;  
  4.     private List<Fragment> list=new ArrayList<Fragment>();  
  5.     private MyFragmentAdapter adapter;  
  6.     private final int[] array=new int[]{R.id.home_follow,R.id.home_recommend,R.id.home_location};  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.view_pager_test);  
  12.           
  13.         FollowFragment topFragment = new FollowFragment();  
  14.         RecommendFragment  hotFragment = new RecommendFragment();  
  15.         LocationFragment locationFragment = new LocationFragment();  
  16.         list.add(topFragment);  
  17.         list.add(hotFragment);  
  18.         list.add(locationFragment);  
  19.           
  20.         vPager = (ViewPager) findViewById(R.id.viewpager_home);  
  21.         adapter = new MyFragmentAdapter(getSupportFragmentManager(), list);  
  22.         vPager.setAdapter(adapter);  
  23.         vPager.setOffscreenPageLimit(2);  
  24.         vPager.setCurrentItem(1);  
  25.         vPager.setOnPageChangeListener(pageChangeListener);  
  26.           
  27.         homeFollow=(RadioButton) findViewById(R.id.home_follow);  
  28.         homeRecommend=(RadioButton) findViewById(R.id.home_recommend);  
  29.         homeLocation=(RadioButton) findViewById(R.id.home_location);  
  30.           
  31.         RadioGroup group=(RadioGroup) findViewById(R.id.home_page_select);  
  32.         group.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  33.             @Override  
  34.             public void onCheckedChanged(RadioGroup group,int checkedId){  
  35.                 //设置了ViewPager的当前item就会触发ViewPager的SimpleOnPageChangeListener监听函数  
  36.                 switch (checkedId){  
  37.                 case R.id.home_follow:  
  38.                     vPager.setCurrentItem(0);  
  39.                     break;  
  40.                 case R.id.home_recommend:  
  41.                     vPager.setCurrentItem(1);  
  42.                     break;  
  43.                 case R.id.home_location:  
  44.                     vPager.setCurrentItem(2);  
  45.                     break;  
  46.                 }  
  47.             }  
  48.         });  
  49.     }  
  50.       
  51.     SimpleOnPageChangeListener pageChangeListener=new SimpleOnPageChangeListener(){  
  52.         public void onPageSelected(int position){  
  53.             change(array[position]);  
  54.         }  
  55.     };  
  56.       
  57.     /** 
  58.      * 改变背景颜色,背景图片 
  59.      * @param checkedId 
  60.      */  
  61.     private void change(int checkedId){  
  62.         //改变背景颜色  
  63.         homeFollow.setBackgroundResource(R.drawable.icon_top_normal);  
  64.         homeRecommend.setBackgroundResource(R.drawable.icon_recommend_normal);  
  65.         homeLocation.setBackgroundResource(R.drawable.icon_location_normal);  
  66.           
  67.         //改变字体颜色  
  68.         homeFollow.setTextColor(getResources().getColor(R.color.white_normal));  
  69.         homeRecommend.setTextColor(getResources().getColor(R.color.white_normal));  
  70.         homeLocation.setTextColor(getResources().getColor(R.color.white_normal));  
  71.           
  72.         switch (checkedId){  
  73.         case R.id.home_follow:  
  74.             homeFollow.setBackgroundResource(R.drawable.icon_top_select);  
  75.             homeFollow.setTextColor(getResources().getColor(R.color.balck_normal));  
  76.             homeFollow.setChecked(true);  
  77.             break;  
  78.         case R.id.home_recommend:  
  79.             homeRecommend.setBackgroundResource(R.drawable.icon_recommend_select);  
  80.             homeRecommend.setTextColor(getResources().getColor(R.color.balck_normal));  
  81.             homeRecommend.setChecked(true);  
  82.             break;  
  83.         case R.id.home_location:  
  84.             homeLocation.setBackgroundResource(R.drawable.icon_location_select);  
  85.             homeLocation.setTextColor(getResources().getColor(R.color.balck_normal));  
  86.             homeLocation.setChecked(true);  
  87.             break;  
  88.         }  
  89.     }  
  90. }  


4.ViewPager适配器

  1. public class MyFragmentAdapter extends FragmentStatePagerAdapter {  
  2.     private List<Fragment>list;  
  3.     public MyFragmentAdapter(FragmentManager fm, List<Fragment> list) {  
  4.         super(fm);  
  5.         this.list = list;  
  6.     }  
  7.   
  8.     public MyFragmentAdapter(FragmentManager fm) {  
  9.         super(fm);  
  10.     }  
  11.   
  12.     @Override  
  13.     public Fragment getItem(int arg0) {  
  14.         return list.get(arg0);  
  15.     }  
  16.   
  17.     @Override  
  18.     public int getCount() {  
  19.         return list.size();  
  20.     }  
  21. }  



5.主界面布局文件

  1. <span style="font-size:14px;"><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical" >  
  5.   
  6.     <android.support.v4.view.ViewPager  
  7.         android:id="@+id/viewpager_home"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent" />  
  10.   
  11.     <RelativeLayout  
  12.         android:id="@+id/login_success_title"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:background="#78000000"  
  16.         android:paddingLeft="5dip"  
  17.         android:paddingRight="5dip" >  
  18.   
  19.         <RadioGroup  
  20.             android:id="@+id/home_page_select"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"  
  23.             android:layout_centerHorizontal="true"  
  24.             android:layout_centerVertical="true"  
  25.             android:orientation="horizontal"  
  26.             android:paddingBottom="4dp"  
  27.             android:paddingTop="4dp" >  
  28.   
  29.             <RadioButton  
  30.                 android:id="@+id/home_follow"  
  31.                 android:background="@drawable/icon_top_normal"  
  32.                 android:button="@null"  
  33.                 android:gravity="center"  
  34.                 android:text="关注"  
  35.                 android:textColor="@color/select_home_radio_color" />  
  36.   
  37.             <RadioButton  
  38.                 android:id="@+id/home_recommend"  
  39.                 android:background="@drawable/icon_recommend_select"  
  40.                 android:button="@null"  
  41.                 android:checked="true"  
  42.                 android:gravity="center"  
  43.                 android:text="推荐"  
  44.                 android:textColor="@color/select_home_radio_color" />  
  45.   
  46.             <RadioButton  
  47.                 android:id="@+id/home_location"  
  48.                 android:background="@drawable/icon_location_normal"  
  49.                 android:button="@null"  
  50.                 android:gravity="center"  
  51.                 android:text="位置"  
  52.                 android:textColor="@color/select_home_radio_color" />  
  53.         </RadioGroup>  
  54.     </RelativeLayout>  
  55.   
  56. </FrameLayout></span>  


 6.效果图如下:

     


还有一些布局文件,跟资源文件我就不贴出来了,有需要的可以直接下载源码

点击下载源码

目录
相关文章
|
JSON 前端开发 Java
SpringBoot项目Http406错误问题解决
一、背景 1、自定义了返回类 2、控制器使用@ResponseBody注解标记
|
Android开发
【Android App】蓝牙的设备配对、音频传输、点对点通信的讲解及实战(附源码和演示 超详细)
【Android App】蓝牙的设备配对、音频传输、点对点通信的讲解及实战(附源码和演示 超详细)
2911 1
|
测试技术
jmeter性能指标分析
使用jmeter压测后,对各项指标进行分析
1566 0
|
Oracle 关系型数据库
Oracle OGG 单表重新初始化同步的两种思路
OGG 单表重新初始化同步的两种思路
4667 0
|
2月前
|
XML JSON API
识别这些API接口定义(http,https,api,RPC,webservice,Restful api ,OpenAPI)
本内容介绍了API相关的术语分类,包括传输协议(HTTP/HTTPS)、接口风格(RESTful、WebService、RPC)及开放程度(API、OpenAPI),帮助理解各类API的特点与应用场景。
|
3月前
|
JSON API 开发者
淘宝 API 零基础快速上手教程(2025 版)
淘宝API是淘宝开放平台提供的接口,允许开发者获取商品、订单等数据,并实现自动化操作。本文介绍了API基础概念、账号开通流程、权限申请、调用方法及实战示例,适合零基础开发者快速入门并掌握淘宝API的核心使用技巧。
|
5月前
|
存储 人工智能 缓存
《记忆革命:Gemini 1.5如何让Transformer突破百万级上下文枷锁》
Gemini 1.5突破传统Transformer上下文限制,实现百万级token处理能力。通过“工作记忆”与“长期记忆”双层结构,结合语义压缩、记忆路由及解耦计算存储,大幅提升效率。它模拟人脑记忆机制,强化长文档理解、代码推理和跨模态关联能力,为AI认知范式带来革命性转变,开启结构化记忆与动态调控新方向。
161 2
|
5月前
|
人工智能 JavaScript API
开发者必备:阿里云百炼 API 调用图文教程
百炼是阿里云推出的大模型服务平台,集成了很多优质的 AI 模型,包括通义千问、DeepSeek 等。
开发者必备:阿里云百炼 API 调用图文教程
|
Python
二:《Python基础语法汇总》— 输入与输出&运算符
【8月更文挑战第16天】本篇文章详细讲述了关于输入输出函数的语法及占位符;转义字符和运算符的使用,并附上详细的代码示例
122 2
|
传感器 XML 物联网
Android项目实战(三十四):蓝牙4.0 BLE 多设备连接
原文:Android项目实战(三十四):蓝牙4.0 BLE 多设备连接   最近项目有个需求,手机设备连接多个蓝牙4.0 设备 并获取这些设备的数据。   查询了很多资料终于实现,现进行总结。   ------------------------------------------------...
1787 1