zoukankan      html  css  js  c++  java
  • 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.效果图如下:

         


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

    点击下载源码




  • 相关阅读:
    IIS是如何处理ASP.NET请求的
    数据库访问性能优化
    通信交互总结
    数据库集群技术漫谈
    VS2010中出现无法嵌入互操作类型
    正则表达式-更新版
    IIS部署SSL证书后提示不可信的解决方案
    CSS水平居中和垂直居中解决方案
    jQuery get/post区别及contentType取值
    配置Tomcat使用https协议
  • 原文地址:https://www.cnblogs.com/yishaochu/p/5078622.html
Copyright © 2011-2022 走看看