zoukankan      html  css  js  c++  java
  • [有源码]ViewPager+Fragment+RadioButton替代TabActivity

    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

    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  [要积分]


  • 相关阅读:
    SpringMVC学习指南【笔记6】JSTL标签、函数
    SpringMVC学习指南【笔记5】EL表达式、实现免脚本JSP页面、禁用EL计算的设置
    SpringMVC学习指南【笔记4】数据绑定、表单标签库、转换器、格式化、验证器
    序列封包和序列解包
    python 字符串分割,连接方法
    Jmeter常用插件(转)
    不同的content-type,Jmeter入参不同
    性能监测(CPU)
    正则表达式
    乱码问题
  • 原文地址:https://www.cnblogs.com/tqj-zyy/p/4559767.html
Copyright © 2011-2022 走看看