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 Listlist=new ArrayList (); 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 Listlist; public MyFragmentAdapter(FragmentManager fm, List 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.主界面布局文件
6.效果图例如以下:
另一些布局文件,跟资源文件我就不贴出来了,有须要的能够直接下载源代码
推荐下自己创建的android QQ群:202928390 欢迎大家的增加.
推荐一个Android开发人员必关注公众号,每周都有原创干货