android slidingmenu的内容切换

姜大牙 发布于 2013/11/15 12:00
阅读 1K+
收藏 2

android slidingmenu的内容切换

切换代码

public void switchContent(final FragmentModel fragmentModel) {

	getSupportFragmentManager().beginTransaction()
			.replace(R.id.content, fragmentModel.getmFragment()).commit(); 
	textView.setText(fragmentModel.getmTitle());
	Handler mhHandler = new Handler();
	mhHandler.postDelayed(new Runnable() {

		@Override
		public void run() {
			getSlidingMenu().showContent();
		}
	}, 50);
	
}
选项卡代码
        public void onCheckedChanged(RadioGroup arg0, int arg1) {
		// TODO Auto-generated method stub
		switch (arg1) {
		case R.id.rb_toutiao:
			goTouTiaoFragment();
			break;
		case R.id.rb_blog:
			goBlogFragment();
			break;
		}
	}

	private void goTouTiaoFragment() {
		// TODO Auto-generated method stub
		if (getActivity() == null)
			return;

		FragmentModel fragmentModel = mfCenter.getTouTiaoFragmentModel();
		if (getActivity() instanceof MainActivity) {
			MainActivity ra = (MainActivity) getActivity();
			ra.switchContent(fragmentModel);
		}
	}

	private void goBlogFragment(){
		if (getActivity() == null)
			return;

		FragmentModel fragmentModel = mfCenter.getBlogFragmentModel();
		if (getActivity() instanceof MainActivity) {
			MainActivity ra = (MainActivity) getActivity();
			ra.switchContent(fragmentModel);
		}
	}

fragmentControl代码

private static FragmentContorlCenter instance;
	private Context mContext;
	private Map<String, FragmentModel> mFragmentMap = new HashMap<String, FragmentModel>();

	private FragmentContorlCenter(Context context) {
		this.mContext = context;

	}

	public static synchronized FragmentContorlCenter getInstance(Context context) {
		if (instance == null) {
			instance = new FragmentContorlCenter(context);

		}
		return instance;

	}

	public FragmentModel getTouTiaoFragmentModel() {
		FragmentModel fragmentModel = mFragmentMap
				.get(FragmentBuilder.TOU_TIAO_FRAGMENT);
		if (fragmentModel == null) {
			fragmentModel = FragmentBuilder.getTouTiaoFragmentModel();
			mFragmentMap.put(FragmentBuilder.TOU_TIAO_FRAGMENT, fragmentModel);
		}
		return fragmentModel;
	}

	public FragmentModel getBlogFragmentModel() {
		FragmentModel fragmentModel = mFragmentMap
				.get(FragmentBuilder.BLOG_FRAGMENT);
		if (fragmentModel == null) {
			fragmentModel = FragmentBuilder.getBlogFragmentModel();
			mFragmentMap.put(FragmentBuilder.BLOG_FRAGMENT, fragmentModel);
		}
		return fragmentModel;
	}

	public FragmentModel getFragmentModel(String name) {
		return mFragmentMap.get(name);
	}

	public void addFragmentModel(String name, FragmentModel fragment) {
		mFragmentMap.put(name, fragment);
	}

	private static class FragmentBuilder {

		public static final String TOU_TIAO_FRAGMENT = "TOU_TIAO_FRAGMENT";
		public static final String BLOG_FRAGMENT = "BLOG_FRAGMENT";

		public static FragmentModel getTouTiaoFragmentModel() {
			TouTiaoFragment mFragment = new TouTiaoFragment();
			FragmentModel fragmentModel = new FragmentModel("头条", mFragment);
			return fragmentModel;

		}

		public static FragmentModel getBlogFragmentModel() {
			BlogFragment mFragment = new BlogFragment();
			FragmentModel fragmentModel = new FragmentModel("Blog", mFragment);
			return fragmentModel;

		}

	}

fragmentModel代码

public class FragmentModel {

	private String mTitle;
	private Fragment mFragment;

	public FragmentModel(String mTitle, Fragment mFragment) {
		this.mTitle = mTitle;
		this.mFragment = mFragment;

	}

	public String getmTitle() {
		return mTitle;
	}

	public Fragment getmFragment() {
		return mFragment;
	}

}

内容fragment继承一个commfragment,作为父类

代码

public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		textView.setText("新闻");
		super.onActivityCreated(savedInstanceState);
		
	}

跪求各位大神,多谢了

加载中
返回顶部
顶部