当前位置: 首页 > news >正文

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.效果图例如以下:

     


另一些布局文件,跟资源文件我就不贴出来了,有须要的能够直接下载源代码

点击下载源代码


推荐下自己创建的android QQ群:202928390 欢迎大家的增加.


推荐一个Android开发人员必关注公众号,每周都有原创干货


转载于:https://www.cnblogs.com/yutingliuyl/p/7102137.html

相关文章:

  • 通过获取异步加载JS文件进度实现一个canvas环形loading图
  • PyTorch快速入门教程五(rnn)
  • 故障排查
  • 腾讯云服务器 安装监控组件
  • CRM系统客户形成需求和认知的五大因素
  • 【leetcode】55. Jump Game
  • node.js 学习(二)
  • 内华达州PUC特准3.2万光伏用户优惠太阳能补贴费率
  • 文件读,写,拷贝,删除
  • 神州数码网真解决方案助山西电力信息高速化
  • 大数据正在改变企业决策方式
  • Centos 7 配置tomcat服务器
  • 常用软件测试工具的分析
  • 让git更高效--文末有福利
  • 力争大数据及关联产业规模2020年达300亿元
  • [译]CSS 居中(Center)方法大合集
  • 「前端早读君006」移动开发必备:那些玩转H5的小技巧
  • 【跃迁之路】【477天】刻意练习系列236(2018.05.28)
  • Android 控件背景颜色处理
  • express如何解决request entity too large问题
  • github从入门到放弃(1)
  • If…else
  • Java 23种设计模式 之单例模式 7种实现方式
  • JavaScript中的对象个人分享
  • MQ框架的比较
  • PHP 小技巧
  • React系列之 Redux 架构模式
  • Redis 懒删除(lazy free)简史
  • Solarized Scheme
  • Spark in action on Kubernetes - Playground搭建与架构浅析
  • SpiderData 2019年2月16日 DApp数据排行榜
  • Spring Security中异常上抛机制及对于转型处理的一些感悟
  • SpringBoot几种定时任务的实现方式
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • TiDB 源码阅读系列文章(十)Chunk 和执行框架简介
  • Vue2 SSR 的优化之旅
  • 后端_MYSQL
  • 理解 C# 泛型接口中的协变与逆变(抗变)
  • 力扣(LeetCode)22
  • 王永庆:技术创新改变教育未来
  • 正则表达式小结
  • 你对linux中grep命令知道多少?
  • MPAndroidChart 教程:Y轴 YAxis
  • 支付宝花15年解决的这个问题,顶得上做出十个支付宝 ...
  • ###C语言程序设计-----C语言学习(3)#
  • (30)数组元素和与数字和的绝对差
  • (C语言版)链表(三)——实现双向链表创建、删除、插入、释放内存等简单操作...
  • (笔试题)分解质因式
  • (附源码)计算机毕业设计ssm电影分享网站
  • (四)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (转) Android中ViewStub组件使用
  • (转)http协议
  • .form文件_SSM框架文件上传篇
  • .net 使用ajax控件后如何调用前端脚本
  • .NET4.0并行计算技术基础(1)