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. 【逆向篇】分析一段简单的ShellCode——从TEB到函数地址获取

    其实分在逆向篇不太合适,因为并没有逆向什么程序. 在http://www.exploit-db.com/exploits/28996/上看到这么一段最简单的ShellCode,其中的技术也是比较常见的 ...

  2. GOOD MEETINGS CREATE SHARED UNDERSTANDING, NOT BRDS!

      Deliverables and artifacts were a focal point of BA work during the early part of my career. If I ...

  3. 关于iOS后台问题( 一 )(ios后台刷新,后台定位,后台下载,真后台)

    关于iOS的后台,以下引用一些文段进行一下脑补,请同学们大致看一下,有个基础,原文出处 -------------------------------------------------------- ...

  4. linux下ftp配置文件详解

    # 匿名用户配置 anonymous_enable=YES # 是否允许匿名ftp,如否则选择NO anon_upload_enable=YES # 匿名用户是否能上传 anon_mkdir_writ ...

  5. SQL Server 2012基本知识

    1:图形化界面设置外键 解决:table->选中表->design->选中需要设置外键的字段->选择"关系"->选择"添加"-&g ...

  6. SQL语句操作数据与一些函数使用的丰富数据库

    数据库有多重要,其实不用我说,但该怎么运用好数据库下SQL语句与其它的如“函数”等等,那就需要我们大家多多去练习并总结其中的窍门,或许你的总结没那么好,担只要你的练习足够多,就算那不是窍门,那也将是你 ...

  7. Java_正则表达式

    1.简介 正则表达式是一串特定字符串,组成一个“规则字符串”,这个“规则字符串”是描述文本规则的工具.正则表达式就是记录文本规则的代码. 2.字符集合 [abc] a,b,c中任意一个字符 [^abc ...

  8. Android 中关于ListView分割线的设置

    今天发现许多App上的listview的item之间的分割线都只显示了右边一部分,而左边的那一半则没有,第一反应则是给分割线设置一张背景图片就ok了: android:divider="@m ...

  9. 编写一个基本的连接池来实现连接的复用&一些工程细节上的优化

    package it.cast.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQL ...

  10. NLP--十项沟通前的思想准备

    如何达到有效沟通?sino NLP课程给我们十项针对沟通前的思想准备,可让我们了解怎样做到效果卓越的沟通: 1.建立和谐气氛. 这是有效沟通的前提条件,只有首先建立一个和谐的气氛,双方才能彼此敞开心扉 ...