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};
@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切换的更多相关文章
- 巧妙实现缺角radiogroup控制多个fragment切换和滑动
在android开发中,用一个radiogroup控制多个fragment切换是十分常见的需求.但是如果fragment是一个ListView,如何保证滑动的时候通过缺角可以看到下面的listview ...
- Fragment里面的ViewPager嵌套subFragment,主Fragment切换的时候subFragment出现空白Fragment的Bug
Fragment第二次进入不显示,主要是第二次加载的时候重复调用了onCreateView()这个方法,重新new了一个pageadapter导致子fragment不显示,问题的解决方法就是在onCr ...
- 使用ViewPager切换Fragment时,防止频繁调用OnCreatView
使用ViewPager切换Fragment,我原先使用系统自带的适配器FragmentPagerAdapter. 切换fragment时,频繁调用oncreatview(). 查看FragmentPa ...
- Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...
- ViewPager -- Fragment 切换卡顿 性能优化
当ViewPager切换到当前的Fragment时,Fragment会加载布局并显示内容,如果用户这时快速切换ViewPager,即 Fragment需要加载UI内容,而又频繁地切换Fragment, ...
- 【Android】保存Fragment切换状态
前言 一般频繁切换Fragment会导致频繁的释放和创建,如果Fragment比较臃肿体验就非常不好了,这里分享一个方法. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.c ...
- Android Studio精彩案例(二)《仿微信动态点击底部tab切换Fragment》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 现在很多的App要么顶部带有tab,要么就底部带有tab.用户通过点击tab从而切换不同的页面(大部分情况时去切换fragment). ...
- Android使用Fragment来实现TabHost的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3360938.html 如新浪微博下面的标签切换功能,我以前也写过一篇博文(http:/ ...
- fragment切换刷新 及下拉刷新
此工程较BaiduLocationXMLFragmentDB相比:1.滑动fragment自动刷新该fragment2.下拉刷新fragment,上拉暂未实现 a.fragment切换刷新 1 . 由 ...
随机推荐
- Android 无标题 全屏设置
标题栏和状态栏 Android程序默认情况下是包含状态栏和标题栏的. 在Eclipse中新建一个Android程序,运行后显示如下: 图中标出了状态栏(显示时间.电池电量.网络等)和标题栏(显示应用的 ...
- 使用GDB 追踪依赖poco的so程序,core dump文件分析.
前言 在windows 下 系统核心态程序蓝屏,会产生dump文件. 用户级程序在设置后,程序崩溃也会产生dump文件.以方便开发者用windbg进行分析. so,linux 系统也有一套这样的东东- ...
- 关于MVC的开源商城 Nop之闲聊
nopcommerce是国外的一个高质量的开源b2c网站系统,基于EntityFramework4.0和MVC3.0,使用Razor模板引擎,有很强的插件机制,包括支付配送功能都是通过插件来实现的,基 ...
- oracle触发器和存储过程的格式
最近接到一个任务要根据一个表来转移另一个表的数据到第三个表.想了想,用决定用触发器+存储过程的方式来做.有些时间没有写存储过程和触发器了,查了一下资料,确定了oracle的触发器和存储过程的格式. 触 ...
- iOS 属性修饰符记录 --不定时更新
重新审视了一下OC在属性修饰符,特意记录一下来.以后不定时更新 > retain:只有在非ARC下才会有效,所有如果在ARC下使用了retain修饰也白搭 如以下的data属性用retain修饰 ...
- PHP基础知识之逻辑运算符
与(and,&&)和或(or,||)有两种形式,两种形式的区别是:优先级不一样,and.or的优先级低于&&.||
- SqlServer游标简介
游标实例: Declare MyCusror Cursor Scroll For Select * From Master_Goods Order By GoodsID Ope ...
- ql 判断 函数 存储过程是否存在的方法
下面为您介绍sql下用了判断各种资源是否存在的代码,需要的朋友可以参考下,希望对您学习sql的函数及数据库能够有所帮助. 库是否存在 if exists(select * from master..s ...
- 通过html和css做出下拉导航栏的效果
通过观察了百度的首页,对于更多产品一栏,觉得可以不涉及JS便可写出下拉导航栏的效果 1.先设计出大体的框架 <div class="nav"> <ul> & ...
- linux 使用fdisk分区扩容
标签:fdisk分区 概述 我们管理的服务器可能会随着业务量的不断增长造成磁盘空间不足的情况,在这个时候我们就需要增加磁盘空间,本章主要介绍如何使用fdisk分区工具创建磁盘分区和挂载分区,介绍两种情 ...