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 . 由 ...
随机推荐
- linux platform设备与驱动
struct platform_driver { int (*probe)(struct platform_device *); int (*remove)(struct platform_devic ...
- 实现Myxls设置行高的功能(转)
MyXLS是一个导出Excel的好工具,速度快,体积小,而且也不用担心使用Com生成Excel时资源释放的问题了.但是作者提供的代码没有设置行高 要实现这个效果,首先需要修改两个文件: 1.Row.c ...
- touches 事件捕获不到
在UIView上加载了一个UIScrollView(全屏),touches 事件捕获不到了 原因:UIView的touch事件被UIScrollView捕获了,无法传递下去 解决方法:写一个UIScr ...
- HTML5 WebSocket
在WebSocket API中,浏览器和服务器只需要做一个握手动作,然后,浏览器和服务器之间就形成一条快速通道,两者之间就可以直接进行数据传送,这一个功能可以应用到"字幕",自己做 ...
- 关于C#的微信开发的入门记录一
在之前老是看到一些微信开发的例子,但是作为初学者会有很多问题,之前我也找了很多帖子,但是最终也没能解决,现在刚好手里有一个项目,总结一下分享给准备做却动不了手的朋友们,本文只是以我个人的经验作为浅谈( ...
- webform 图片验证码制作
界面:1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.as ...
- -[NSNull countByEnumeratingWithState:objects:count:]:
当数组为空时遍历数组容易出这样的问题, -[NSNull countByEnumeratingWithState:objects:count:]: unrecognized selector sent ...
- Visual Studio 2015 如何将全英界面转成中文
1 启动VS2015程序,在菜单栏中找到tools 2 在弹出的下拉窗口中选中options 3 此时弹出的对话框,选中Environment下的international setting 4 点击获 ...
- Ajax全面基础学习(一)
快捷方法: $.get(url,[data],[callback],[type])get方法的[data]将被链在url后面[callback]是请求成功后的回调,可以得到响应数据,如果请求失败,看不 ...
- DOMO1
以下是Demo首页的预览图 demo下载:http://www.eoeandroid.com/forum.php?mod=attachment&aid=NjE0Njh8ZTIyZDA2M2N8 ...