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. Yii框架CURD方法

    在YII框架中,CURD有2种方式: 1.AR模式:2. DAO模式 AR模式下 查全部   MODEL  $model->find()->asArray()->all()查单 个  ...

  2. yoman 创建generator

    yoman作为一个模板工具,能够创建自己的模板,下面具体介绍下. 首先 安装一个模板工具(npm install -g generator-generator),此工具会自动创建一些必要的文件.安装完 ...

  3. 转载:Chrome调试折腾记_(1)调试控制中心快捷键详解!!!

    转载:http://blog.csdn.net/crper/article/details/48098625 大多浏览器的调试功能的启用快捷键都一致…按下F12;还是熟悉的味道;  或者直接 Ctrl ...

  4. AngularJS中的route可以控制页面元素的改变,使多页面变成一个单页面。。。

    SPA(Single Page Application)指的是通单一页面展示所有功能,通过Ajax动态获取数据然后进行实时渲染,结合CSS3动画模仿原生App交互,然后再进行打包(使用工具把Web应用 ...

  5. #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #endif

    情况1:#ifdef _DEBUGvirtual void AssertValid() const;                 //assert(断言)valid(有效的,正确的)virtual ...

  6. 五、jquery使用工具函数

    工具函数对应的网址在 http://api.jquery.com/categouy/utilities/   工具函数处理对象的不同,可以将其分为几大类别:浏览器的检测.数组和对象的操作.字符串的操作 ...

  7. C/C++_date&time

    在标准C/C++中与日期和时间相关的数据结构 注意:年份是实际年份与  1900  的差值 我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下: #ifndef _TM_DEFIN ...

  8. properties文件使用{0}...

    例如properties文件的配置 weixin.token.url=https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credent ...

  9. Android课程---序列化与反序列化(转)

    ava序列化与反序列化是什么?为什么需要序列化与反序列化?如何实现Java序列化与反序列化?本文围绕这些问题进行了探讨. 1.Java序列化与反序列化 Java序列化是指把Java对象转换为字节序列的 ...

  10. jQuery中设置form表单中action值与js有什么不同。。。。

    jQuery中设置form表单中action值与js有什么不同.... HTML代码如下: <form action="" method="post" i ...