Fragment加Viewpager的使用小结

先把布局放上来比较简单




    
    

    

        

        

        

        

        
    


viewpager为内容需要填充的版块,布局的显示是这样的

Fragment加Viewpager的使用小结_第1张图片

接下来就是几个子的fragment 布局随便定 重要的是继承fragment,就这重要的就是继承FragmentActivity这个Activity了

下面贴上该Activity的代码!!!
public class MainActivity extends FragmentActivity implements OnPageChangeListener  {
	// 四个滑动页面
	private ViewPager mViewPager;
	private FragmentPagerAdapter mAdapter;
	private List mDatas;
	private RadioGroup rg_main_btns;
	private RadioButton mainHomeTab;
	// 控件
	

	private int mScreen1_4;

	/** 当前视图宽度 **/
	private Integer viewPagerW = 0;

	private static int width = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//initLine();
		initView();
		initFragment();
	}


	/**
	 * 初始化控件
	 */
	private void initView() {

		rg_main_btns=(RadioGroup) findViewById(R.id.rg_main_btns);
		mainHomeTab=(RadioButton) findViewById(R.id.mainHomeTab);
		mViewPager = (ViewPager) findViewById(R.id.vPager);
		mDatas = new ArrayList();
		rg_main_btns.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup arg0, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.mainHomeTab:
					mViewPager.setCurrentItem(0);
					break;
				case R.id.mainNewsTab:
					mViewPager.setCurrentItem(1);
					break;
				case R.id.mainRoomTab:
					mViewPager.setCurrentItem(2);
					break;
				case R.id.mainVideoTab:
					mViewPager.setCurrentItem(3);
					break;
				case R.id.mainPersonTab:
					mViewPager.setCurrentItem(4);
					break;

				default:
					break;
				}
			}
		});

		

	}

	/**
	 * 初始化fragment
	 */
	private void initFragment() {
		PageFirstActivity mSDF = new PageFirstActivity();
		PageNewsActivity mCPF = new PageNewsActivity();
		PageVideoActivity mSSF = new PageVideoActivity();
		PageRoomActivity mPPF = new PageRoomActivity();
		MeActivity tt = new MeActivity();
		mDatas.add(mSDF);
		mDatas.add(mCPF);
		mDatas.add(mSSF);
		mDatas.add(mPPF);
		mDatas.add(tt);

		mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

			@Override
			public int getCount() {
				return mDatas == null ? 0 : mDatas.size();
			}

			@Override
			public Fragment getItem(int position) {
				return mDatas.get(position);
			}
		};
		mViewPager.setAdapter(mAdapter);
		mViewPager.setCurrentItem(0);

	}

	@Override
	public void onPageScrollStateChanged(int position) {

	}

	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

	}

	/**
	 * 获取屏幕宽度
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenWidth(Context context) {
		if (width == 0) {
			WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
			Display display = manager.getDefaultDisplay();
			width = display.getWidth();
		}
		return width;
	}


	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		
	}

}

实现这个效果就是定义一个List< Fragment> 然后向里面加Fragment再填充 FragmentPagerAdapter,viewpager setAdpater基本就ok了
通过viewpager的setCurrentItem()选取就ok了

个人小结,有错误请指出谢谢!!!

你可能感兴趣的:(Android学习整理,android,布局)