自定义Tabs

public class MainActivity extends FragmentActivity {

	private ViewPager vp;
	
	private RadioGroup rg;
	
	private RadioButton rb1,rb2,rb3,rb4,rb5,rb6,rb7,rb8,rb9;
	private RadioButton[] rbs=new RadioButton[9];
	
	private HorizontalScrollView hs;
	//缩放
	private Animation anim;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		

		//控件实例化
		init();
		
		//选中RadioButton的时候,ViewPager需要显示对应页
		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				//被选中的RadioButton
				RadioButton rb=(RadioButton) findViewById(checkedId);
				
				for(int i=0;i<rbs.length;i++){
					if(rbs[i]==rb){
						//应该ViewPager显示第i页
						vp.setCurrentItem(i);
						//把被选中的RadioButton的文本颜色--》红色
						rbs[i].setTextColor(Color.parseColor("#ff0000"));
						//让被选中的RadioButton执行缩放动画
						rbs[i].startAnimation(anim);
						
					}else{
						rbs[i].setTextColor(Color.parseColor("#000000"));
						
						//当对应的RadioButton不被选中的时候--》清除动画
						rbs[i].clearAnimation();
					}
				}
			}
		});
		
		
		//给ViewPager设置Adapter
		vp.setAdapter(new VPAdapter(getSupportFragmentManager()));
		
		//给ViewPager添加翻页监听
		vp.setOnPageChangeListener(new OnPageChangeListener() {
			//页面被选中---》当前显示
			@Override
			public void onPageSelected(int position) {
				rbs[position].setChecked(true);
				
				int left = rbs[position].getLeft();
				int right = rbs[position].getRight();
				int width=getWindowManager().getDefaultDisplay().getWidth();
				
				hs.scrollTo((left-width/2)+(right-left)/2, 0);
			}
			
			//滑动过程中调用方法:参数1:位置值,要切换的两个page中左边的page的position
			//参数2:偏移量0-1,要切换的两个page中左边的page的偏移量
			//参数3:偏移量像素值,要切换的两个page中左边的page的偏移量
			
			@Override
			public void onPageScrolled(int position, float arg1, int arg2) {
//				System.out.println("position="+position+",offset="+arg1+",offsetpx="+arg2);
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		//开启动画
		rb1.startAnimation(anim);
	}
	
	/**控件实例化*/
	private void init() {
		
		hs=(HorizontalScrollView) findViewById(R.id.hs);
		vp=(ViewPager) findViewById(R.id.vp);
		rg=(RadioGroup) findViewById(R.id.rg);	
		
		anim=AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
		
		
		rb1=(RadioButton) findViewById(R.id.rb1);
		
		//设置rb1默认选中
		rb1.setTextColor(Color.RED);
		
		rb2=(RadioButton) findViewById(R.id.rb2);
		rb3=(RadioButton) findViewById(R.id.rb3);
		rb4=(RadioButton) findViewById(R.id.rb4);
		rb5=(RadioButton) findViewById(R.id.rb5);
		rb6=(RadioButton) findViewById(R.id.rb6);
		rb7=(RadioButton) findViewById(R.id.rb7);
		rb8=(RadioButton) findViewById(R.id.rb8);
		rb9=(RadioButton) findViewById(R.id.rb9);
		
		rbs[0]=rb1;
		rbs[1]=rb2;
		rbs[2]=rb3;
		rbs[3]=rb4;
		rbs[4]=rb5;
		rbs[5]=rb6;
		rbs[6]=rb7;
		rbs[7]=rb8;
		rbs[8]=rb9;
	}
	//ViewPager的adapter
	class VPAdapter extends FragmentPagerAdapter{

		public VPAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			return FragmentPage.getInstance(position);
		}

		@Override
		public int getCount() {
			return 9;
		}
		
	}

}

package com.example.day26demo07tablinevp.fragments;

import com.example.day26demo08tabs.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class FragmentPage extends Fragment{
	
	private static  int[] imgs={R.drawable.p1,R.drawable.p2,R.drawable.p3,R.drawable.p4,R.drawable.p5,R.drawable.p6,R.drawable.p7,R.drawable.p8,R.drawable.p9};
	
	
	public static FragmentPage getInstance(int position){
		
		FragmentPage fragment = new FragmentPage();
		
		Bundle bundle = new Bundle();
		bundle.putInt("position", position);
		//传递参数
		fragment.setArguments(bundle);
		
		return fragment;
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		ImageView imgv = new ImageView(getActivity());
		
		if(getArguments()!=null){
			//设置显示的图片
			imgv.setBackgroundResource(imgs[getArguments().getInt("position")]);
		}else{
			imgv.setBackgroundResource(R.drawable.ic_launcher);
		}
		
		return imgv;
	}
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:id="@+id/hs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" >

        <RadioGroup
            android:id="@+id/rg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rb1"
                style="@style/rb"
                android:text="头条" />

            <RadioButton
                android:id="@+id/rb2"
                style="@style/rb"
                android:text="今天" />

            <RadioButton
                android:id="@+id/rb3"
                style="@style/rb"
                android:text="明天" />

            <RadioButton
                android:id="@+id/rb4"
                style="@style/rb"
                android:text="明天1" />

            <RadioButton
                android:id="@+id/rb5"
                style="@style/rb"
                android:text="明天2" />

            <RadioButton
                android:id="@+id/rb6"
                style="@style/rb"
                android:text="明天3" />

            <RadioButton
                android:id="@+id/rb7"
                style="@style/rb"
                android:text="明天4" />

            <RadioButton
                android:id="@+id/rb8"
                style="@style/rb"
                android:text="明天5" />

            <RadioButton
                android:id="@+id/rb9"
                style="@style/rb"
                android:text="明天6" />
        </RadioGroup>
    </HorizontalScrollView>

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="20dp" >
    </android.support.v4.view.ViewPager>

</LinearLayout>

原文地址:https://www.cnblogs.com/merbn/p/4542240.html