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开发人员必关注公众号,每周都有原创干货


posted on 2017-07-01 13:29  yutingliuyl  阅读(157)  评论(0编辑  收藏  举报