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}; @Override
protected 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() {
@Override
public 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);
} @Override
public Fragment getItem(int arg0) {
return list.get(arg0);
} @Override
public 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.ViewPager
android:id="@+id/viewpager_home"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <RelativeLayout
android:id="@+id/login_success_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#78000000"
android:paddingLeft="5dip"
android:paddingRight="5dip" > <RadioGroup
android: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" > <RadioButton
android: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" /> <RadioButton
android: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" /> <RadioButton
android: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.效果图如下:

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

点击下载源码

ViewPager+RadioGroup实现标题栏切换,Fragment切换的更多相关文章

  1. 巧妙实现缺角radiogroup控制多个fragment切换和滑动

    在android开发中,用一个radiogroup控制多个fragment切换是十分常见的需求.但是如果fragment是一个ListView,如何保证滑动的时候通过缺角可以看到下面的listview ...

  2. Fragment里面的ViewPager嵌套subFragment,主Fragment切换的时候subFragment出现空白Fragment的Bug

    Fragment第二次进入不显示,主要是第二次加载的时候重复调用了onCreateView()这个方法,重新new了一个pageadapter导致子fragment不显示,问题的解决方法就是在onCr ...

  3. 使用ViewPager切换Fragment时,防止频繁调用OnCreatView

    使用ViewPager切换Fragment,我原先使用系统自带的适配器FragmentPagerAdapter. 切换fragment时,频繁调用oncreatview(). 查看FragmentPa ...

  4. Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...

  5. ViewPager -- Fragment 切换卡顿 性能优化

    当ViewPager切换到当前的Fragment时,Fragment会加载布局并显示内容,如果用户这时快速切换ViewPager,即 Fragment需要加载UI内容,而又频繁地切换Fragment, ...

  6. 【Android】保存Fragment切换状态

    前言 一般频繁切换Fragment会导致频繁的释放和创建,如果Fragment比较臃肿体验就非常不好了,这里分享一个方法. 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.c ...

  7. Android Studio精彩案例(二)《仿微信动态点击底部tab切换Fragment》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 现在很多的App要么顶部带有tab,要么就底部带有tab.用户通过点击tab从而切换不同的页面(大部分情况时去切换fragment). ...

  8. Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信

    以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3360938.html 如新浪微博下面的标签切换功能,我以前也写过一篇博文(http:/ ...

  9. fragment切换刷新 及下拉刷新

    此工程较BaiduLocationXMLFragmentDB相比:1.滑动fragment自动刷新该fragment2.下拉刷新fragment,上拉暂未实现 a.fragment切换刷新 1 . 由 ...

随机推荐

  1. 模块化管理工具兼打包工具 webpack

    webpack 是一个[模块化管理工具]兼[打包工具] 是一个工具(和seajs,requirejs管理前端模块的方式是不一样) 在webpack一个文件就是一个模块! seajs,requirejs ...

  2. 学用了QT觉得QT较怪异

    如果让我选择不会用qt 还是用界面开源库,boost.

  3. Debian 8 jessie, OpenSSH ssh connection server responded Algorithm negotiation failed

    安装了debian 8.5 就出问题了. root@debian8:~# lsb_release -aNo LSB modules are available.Distributor ID: Debi ...

  4. Handlebars.js的学习

    写在开头的话: 在使用Ghost搭建自己的博客的时候,发现不会Handlebars.js寸步难行,所以本人决定学习下Handlebars.js,因此在此做个记录 为什么选择Handlebars.js ...

  5. 华为oj 字符串最后一个单词的长度

    <img alt="http://img.bbs.csdn.net/upload/201508/06/1438867109_670158.jpg" src="htt ...

  6. 另类的表单数据"序列化"

    背景:最近在做项目时,由于编辑页面需要提交的数据量有些多,而且在提交前还需要做一些逻辑处理,所以如果按照正常的方式,一个个的获取值然后拼接json对象传到后台的话相对工作量较大,而且容易出错,后期的维 ...

  7. RecylerView完美实现瀑布流效果

    RecylerView包含三种布局管理器,分别是LinearLayoutManager,GridLayoutManager,StaggeredGridLayoutManager,对应实现单行列表,多行 ...

  8. ASP.NET Core Linux下为 dotnet 创建守护进程(必备知识)

    前言 在上篇文章中介绍了如何在 Docker 容器中部署我们的 asp.net core 应用程序,本篇主要是怎么样为我们在 Linux 或者 macOs 中部署的 dotnet 程序创建一个守护进程 ...

  9. ABP框架理论学习之后台工作(Jobs)和后台工作者(Workers)

    返回总目录 本篇目录 介绍 后台工作 后台工作者 让你的应用程序一直运行 介绍 ABP提供了后台工作和后台工作者,它们会在应用程序的后台线程中执行一些任务. 后台工作 后台工作以队列和持续的方式在后台 ...

  10. 【php爬虫】百万级别知乎用户数据爬取与分析

    代码托管地址:https://github.com/hoohack/zhihuSpider 这次抓取了110万的用户数据,数据分析结果如下: 开发前的准备 安装Linux系统(Ubuntu14.04) ...