【有源码】ViewPager+Fragment+RadioButton替代TabActivity

From:http://blog.csdn.net/qq634416025/article/details/12677681

 

  1. package cn.com.jdsc;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.FragmentActivity;  
  5. import android.support.v4.view.ViewPager;  
  6. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.RadioButton;  
  10.   
  11. public class MainActivity extends FragmentActivity implements OnClickListener{  
  12.     public static final int TAB_HOME = 0;  
  13.     public static final int TAB_CATAGORY = 1;  
  14.     public static final int TAB_CAR = 2;  
  15.     public static final int TAB_BUY = 3;  
  16.     public static final int TAB_MORE = 4;  
  17.   
  18.     private ViewPager viewPager;  
  19.     private RadioButton main_tab_home, main_tab_catagory, main_tab_car,  
  20.             main_tab_buy, main_tab_more;  
  21.   
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.fragmentmain);  
  26.         initView();  
  27.         addListener();  
  28.     }  
  29.   
  30.     private void initView() {  
  31.         viewPager = (ViewPager) findViewById(R.id.viewpager);  
  32.         main_tab_home = (RadioButton) findViewById(R.id.main_tab_home);  
  33.         main_tab_catagory = (RadioButton) findViewById(R.id.main_tab_catagory);  
  34.         main_tab_car = (RadioButton) findViewById(R.id.main_tab_car);  
  35.         main_tab_buy = (RadioButton) findViewById(R.id.main_tab_buy);  
  36.         main_tab_more = (RadioButton) findViewById(R.id.main_tab_more);  
  37.         main_tab_home.setOnClickListener(this);  
  38.         main_tab_catagory.setOnClickListener(this);  
  39.         main_tab_car.setOnClickListener(this);  
  40.         main_tab_buy.setOnClickListener(this);  
  41.         main_tab_more.setOnClickListener(this);  
  42.           
  43.         FragmentAdapter adapter = new FragmentAdapter(  
  44.                 getSupportFragmentManager());  
  45.         viewPager.setAdapter(adapter);  
  46.     }  
  47.   
  48.     private void addListener() {  
  49.         viewPager.setOnPageChangeListener(new OnPageChangeListener() {  
  50.   
  51.             @Override  
  52.             public void onPageSelected(int id) {  
  53.                 switch (id) {  
  54.                 case TAB_HOME:  
  55.                     main_tab_home.setChecked(true);  
  56.                     break;  
  57.                 case TAB_CATAGORY:  
  58.                     main_tab_catagory.setChecked(true);  
  59.                     break;  
  60.                 case TAB_CAR:  
  61.                     main_tab_car.setChecked(true);  
  62.                     break;  
  63.                 case TAB_BUY:  
  64.                     main_tab_buy.setChecked(true);  
  65.                     break;  
  66.                 case TAB_MORE:  
  67.                     main_tab_more.setChecked(true);  
  68.                     break;  
  69.   
  70.                 default:  
  71.                     break;  
  72.                 }  
  73.             }  
  74.   
  75.             @Override  
  76.             public void onPageScrolled(int arg0, float arg1, int arg2) {  
  77.   
  78.             }  
  79.   
  80.             @Override  
  81.             public void onPageScrollStateChanged(int arg0) {  
  82.   
  83.             }  
  84.         });  
  85.     }  
  86.   
  87.     @Override  
  88.     public void onClick(View v) {  
  89.         switch (v.getId()) {  
  90.         case R.id.main_tab_home:  
  91.             viewPager.setCurrentItem(TAB_HOME);  
  92.             break;  
  93.         case R.id.main_tab_catagory:  
  94.             viewPager.setCurrentItem(TAB_CATAGORY);  
  95.             break;  
  96.         case R.id.main_tab_car:  
  97.             viewPager.setCurrentItem(TAB_CAR);  
  98.             break;  
  99.         case R.id.main_tab_buy:  
  100.             viewPager.setCurrentItem(TAB_BUY);  
  101.             break;  
  102.         case R.id.main_tab_more:  
  103.             viewPager.setCurrentItem(TAB_MORE);  
  104.             break;  
  105.   
  106.         default:  
  107.             break;  
  108.         }         
  109.     }  
  110. }  
package cn.com.jdsc;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;

public class MainActivity extends FragmentActivity implements OnClickListener{
	public static final int TAB_HOME = 0;
	public static final int TAB_CATAGORY = 1;
	public static final int TAB_CAR = 2;
	public static final int TAB_BUY = 3;
	public static final int TAB_MORE = 4;

	private ViewPager viewPager;
	private RadioButton main_tab_home, main_tab_catagory, main_tab_car,
			main_tab_buy, main_tab_more;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragmentmain);
		initView();
		addListener();
	}

	private void initView() {
		viewPager = (ViewPager) findViewById(R.id.viewpager);
		main_tab_home = (RadioButton) findViewById(R.id.main_tab_home);
		main_tab_catagory = (RadioButton) findViewById(R.id.main_tab_catagory);
		main_tab_car = (RadioButton) findViewById(R.id.main_tab_car);
		main_tab_buy = (RadioButton) findViewById(R.id.main_tab_buy);
		main_tab_more = (RadioButton) findViewById(R.id.main_tab_more);
		main_tab_home.setOnClickListener(this);
		main_tab_catagory.setOnClickListener(this);
		main_tab_car.setOnClickListener(this);
		main_tab_buy.setOnClickListener(this);
		main_tab_more.setOnClickListener(this);
		
		FragmentAdapter adapter = new FragmentAdapter(
				getSupportFragmentManager());
		viewPager.setAdapter(adapter);
	}

	private void addListener() {
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int id) {
				switch (id) {
				case TAB_HOME:
					main_tab_home.setChecked(true);
					break;
				case TAB_CATAGORY:
					main_tab_catagory.setChecked(true);
					break;
				case TAB_CAR:
					main_tab_car.setChecked(true);
					break;
				case TAB_BUY:
					main_tab_buy.setChecked(true);
					break;
				case TAB_MORE:
					main_tab_more.setChecked(true);
					break;

				default:
					break;
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.main_tab_home:
			viewPager.setCurrentItem(TAB_HOME);
			break;
		case R.id.main_tab_catagory:
			viewPager.setCurrentItem(TAB_CATAGORY);
			break;
		case R.id.main_tab_car:
			viewPager.setCurrentItem(TAB_CAR);
			break;
		case R.id.main_tab_buy:
			viewPager.setCurrentItem(TAB_BUY);
			break;
		case R.id.main_tab_more:
			viewPager.setCurrentItem(TAB_MORE);
			break;

		default:
			break;
		}		
	}
}

各个Fragment

  1. package cn.com.jdsc;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v4.app.Fragment;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8.   
  9. public class HomeFragment extends Fragment  {  
  10.     @Override  
  11.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  12.             Bundle savedInstanceState) {  
  13.         System.out.println("HomeFragment");  
  14.         return inflater.inflate(R.layout.main_home, container, false);  
  15.     }  
  16. }  
package cn.com.jdsc;

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

public class HomeFragment extends Fragment	{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		System.out.println("HomeFragment");
		return inflater.inflate(R.layout.main_home, container, false);
	}
}





下载地址1:http://www.400gb.com/file/64566349  [无需积分]

下载地址2:http://download.csdn.net/detail/qq634416025/6392371  [要积分

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值