ViewPager+RadioGroup实现标题栏切换,Fragment切换

1.说明:

在使用RadioGroup做标题栏切换的时候,跟ViewPager的滑动有冲突,最后查看了源码+断点调试解决了一些碰到的问题,写一篇博客总结一下,有同样需求的朋友可以借鉴一下,自己以后有用到也方便复习。


2.代码结构,以及功能说明

    1).主界面的Fragment切换使用ViewPager实现

    2).标题栏用RadioGroup实现

    3).实现这两个控件的监听函数,改变背景,改变字体颜色,设置当前Fragment,设置当前选中RadioButton


3.主界面代码实现

  1. public class MainActivity extends FragmentActivity {  
  2.     private RadioButton homeFollow,homeRecommend,homeLocation;  
  3.     private ViewPager  vPager;  
  4.     private List<Fragment> list=new ArrayList<Fragment>();  
  5.     private MyFragmentAdapter adapter;  
  6.     private final int[] array=new int[]{R.id.home_follow,R.id.home_recommend,R.id.home_location};  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.view_pager_test);  
  12.           
  13.         FollowFragment topFragment = new FollowFragment();  
  14.         RecommendFragment  hotFragment = new RecommendFragment();  
  15.         LocationFragment locationFragment = new LocationFragment();  
  16.         list.add(topFragment);  
  17.         list.add(hotFragment);  
  18.         list.add(locationFragment);  
  19.           
  20.         vPager = (ViewPager) findViewById(R.id.viewpager_home);  
  21.         adapter = new MyFragmentAdapter(getSupportFragmentManager(), list);  
  22.         vPager.setAdapter(adapter);  
  23.         vPager.setOffscreenPageLimit(2);  
  24.         vPager.setCurrentItem(1);  
  25.         vPager.setOnPageChangeListener(pageChangeListener);  
  26.           
  27.         homeFollow=(RadioButton) findViewById(R.id.home_follow);  
  28.         homeRecommend=(RadioButton) findViewById(R.id.home_recommend);  
  29.         homeLocation=(RadioButton) findViewById(R.id.home_location);  
  30.           
  31.         RadioGroup group=(RadioGroup) findViewById(R.id.home_page_select);  
  32.         group.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  33.             @Override  
  34.             public void onCheckedChanged(RadioGroup group,int checkedId){  
  35.                 //设置了ViewPager的当前item就会触发ViewPager的SimpleOnPageChangeListener监听函数  
  36.                 switch (checkedId){  
  37.                 case R.id.home_follow:  
  38.                     vPager.setCurrentItem(0);  
  39.                     break;  
  40.                 case R.id.home_recommend:  
  41.                     vPager.setCurrentItem(1);  
  42.                     break;  
  43.                 case R.id.home_location:  
  44.                     vPager.setCurrentItem(2);  
  45.                     break;  
  46.                 }  
  47.             }  
  48.         });  
  49.     }  
  50.       
  51.     SimpleOnPageChangeListener pageChangeListener=new SimpleOnPageChangeListener(){  
  52.         public void onPageSelected(int position){  
  53.             change(array[position]);  
  54.         }  
  55.     };  
  56.       
  57.     /** 
  58.      * 改变背景颜色,背景图片 
  59.      * @param checkedId 
  60.      */  
  61.     private void change(int checkedId){  
  62.         //改变背景颜色  
  63.         homeFollow.setBackgroundResource(R.drawable.icon_top_normal);  
  64.         homeRecommend.setBackgroundResource(R.drawable.icon_recommend_normal);  
  65.         homeLocation.setBackgroundResource(R.drawable.icon_location_normal);  
  66.           
  67.         //改变字体颜色  
  68.         homeFollow.setTextColor(getResources().getColor(R.color.white_normal));  
  69.         homeRecommend.setTextColor(getResources().getColor(R.color.white_normal));  
  70.         homeLocation.setTextColor(getResources().getColor(R.color.white_normal));  
  71.           
  72.         switch (checkedId){  
  73.         case R.id.home_follow:  
  74.             homeFollow.setBackgroundResource(R.drawable.icon_top_select);  
  75.             homeFollow.setTextColor(getResources().getColor(R.color.balck_normal));  
  76.             homeFollow.setChecked(true);  
  77.             break;  
  78.         case R.id.home_recommend:  
  79.             homeRecommend.setBackgroundResource(R.drawable.icon_recommend_select);  
  80.             homeRecommend.setTextColor(getResources().getColor(R.color.balck_normal));  
  81.             homeRecommend.setChecked(true);  
  82.             break;  
  83.         case R.id.home_location:  
  84.             homeLocation.setBackgroundResource(R.drawable.icon_location_select);  
  85.             homeLocation.setTextColor(getResources().getColor(R.color.balck_normal));  
  86.             homeLocation.setChecked(true);  
  87.             break;  
  88.         }  
  89.     }  
  90. }  


4.ViewPager适配器

  1. public class MyFragmentAdapter extends FragmentStatePagerAdapter {  
  2.     private List<Fragment>list;  
  3.     public MyFragmentAdapter(FragmentManager fm, List<Fragment> list) {  
  4.         super(fm);  
  5.         this.list = list;  
  6.     }  
  7.   
  8.     public MyFragmentAdapter(FragmentManager fm) {  
  9.         super(fm);  
  10.     }  
  11.   
  12.     @Override  
  13.     public Fragment getItem(int arg0) {  
  14.         return list.get(arg0);  
  15.     }  
  16.   
  17.     @Override  
  18.     public int getCount() {  
  19.         return list.size();  
  20.     }  
  21. }  



5.主界面布局文件

  1. <span style="font-size:14px;"><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical" >  
  5.   
  6.     <android.support.v4.view.ViewPager  
  7.         android:id="@+id/viewpager_home"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent" />  
  10.   
  11.     <RelativeLayout  
  12.         android:id="@+id/login_success_title"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:background="#78000000"  
  16.         android:paddingLeft="5dip"  
  17.         android:paddingRight="5dip" >  
  18.   
  19.         <RadioGroup  
  20.             android:id="@+id/home_page_select"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"  
  23.             android:layout_centerHorizontal="true"  
  24.             android:layout_centerVertical="true"  
  25.             android:orientation="horizontal"  
  26.             android:paddingBottom="4dp"  
  27.             android:paddingTop="4dp" >  
  28.   
  29.             <RadioButton  
  30.                 android:id="@+id/home_follow"  
  31.                 android:background="@drawable/icon_top_normal"  
  32.                 android:button="@null"  
  33.                 android:gravity="center"  
  34.                 android:text="关注"  
  35.                 android:textColor="@color/select_home_radio_color" />  
  36.   
  37.             <RadioButton  
  38.                 android:id="@+id/home_recommend"  
  39.                 android:background="@drawable/icon_recommend_select"  
  40.                 android:button="@null"  
  41.                 android:checked="true"  
  42.                 android:gravity="center"  
  43.                 android:text="推荐"  
  44.                 android:textColor="@color/select_home_radio_color" />  
  45.   
  46.             <RadioButton  
  47.                 android:id="@+id/home_location"  
  48.                 android:background="@drawable/icon_location_normal"  
  49.                 android:button="@null"  
  50.                 android:gravity="center"  
  51.                 android:text="位置"  
  52.                 android:textColor="@color/select_home_radio_color" />  
  53.         </RadioGroup>  
  54.     </RelativeLayout>  
  55.   
  56. </FrameLayout></span>  


 6.效果图如下:

     


还有一些布局文件,跟资源文件我就不贴出来了,有需要的可以直接下载源码

点击下载源码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值