Fragment切换问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Fragment切换问题相关的知识,希望对你有一定的参考价值。

片断一: add  hind

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rb_common_frame://常用框架
position = 0;
break;
case R.id.rb_thirdparty://第三方
position = 1;
break;
case R.id.rb_custom://自定义
position = 2;
break;
case R.id.rb_other://其他
position = 3;
break;
default:
position = 0;
break;
}

//根据位置得到对应的Fragment
BaseFragment to = getFragment();
//替换
switchFrament(mContent,to);
/**
*
* @param from 刚显示的Fragment,马上就要被隐藏了
* @param to 马上要切换到的Fragment,一会要显示
*/
private void switchFrament(Fragment from,Fragment to) {
if(from != to){
mContent = to;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//才切换
//判断有没有被添加
if(!to.isAdded()){
//to没有被添加
//from隐藏
if(from != null){
ft.hide(from);
}
//添加to
if(to != null){
ft.add(R.id.fl_content,to).commit();
}
}else{
//to已经被添加
// from隐藏
if(from != null){
ft.hide(from);
}
//显示to
if(to != null){
ft.show(to).commit();
}
}
}

}

/**
* 根据位置得到对应的Fragment
* @return
*/
private BaseFragment getFragment() {
BaseFragment fragment = mBaseFragment.get(position);
return fragment;
}
片断二:
 private void switchFrament(BaseFragment fragment) {
//1.得到FragmentManger
FragmentManager fm = getSupportFragmentManager();
//2.开启事务
FragmentTransaction transaction = fm.beginTransaction();
//3.替换
transaction.replace(R.id.fl_content, fragment);
//4.提交事务
transaction.commit();
}

片断三:
//初始化Fragmet
private void initFragment() {
fragmentList.clear();
ItemFragmentOrder itemFragmentOrder = new ItemFragmentOrder();
ItemFragmentIntent itemFragmentIntent = new ItemFragmentIntent();
fragmentList.add(itemFragmentOrder);
fragmentList.add(itemFragmentIntent);
}
//初始化ViewPager
private void initViewPager() {
mPageAdapter = new MainFragmentPageAdapter(activity.getSupportFragmentManager());
viewPager.setAdapter(mPageAdapter);
viewPager.setOffscreenPageLimit(fragmentList.size());
}
//fragmentViewPaager适配器
class MainFragmentPageAdapter extends FragmentPagerAdapter {

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

@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}

@Override
public int getCount() {
return fragmentList.size();
}
}

//切换页面
public void showFragment(int position) {
viewPager.setCurrentItem(position, false);
}
片断四:
@Override
public Fragment getItem(int position) {
BaseFragment fragment = null;
switch (fragmentFlag) {
case FragmentMainVp:
switch (position) {
case MainActivity.fristPage:
fragment = new FragmentFirst();
break;
case MainActivity.order:
fragment = new FragmentOrder();
break;
case MainActivity.mine:
fragment = new FragmentMine();
break;
}//end of switch
break;
}//end of switch
Bundle bundle = new Bundle();
bundle.putInt(BaseFragment.ParamIndex, position);
if (null != paramBundle) bundle.putBundle(BaseFragment.ParamBundle, paramBundle);
if (null != fragment) fragment.setArguments(bundle);
if (null != fragmentAry) fragmentAry[position] = fragment;
return fragment;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);//得到缓存的fragment
if (needReload && null != fragment && null != fm) {
String fragmentTag = fragment.getTag();//得到tag,这点很重要
FragmentTransaction ft = fm.beginTransaction();
if (null != ft) {
ft.remove(fragment);//移除旧的fragment
fragment = getItem(position);//换成新的fragment
ft.add(container.getId(), fragment, fragmentTag);//添加新fragment时必须用前面获得的tag,这点很重要
ft.attach(fragment);
fragment.setUserVisibleHint(true);
ft.commit();
}//end of if
if ((getCount() - 1) == position)
needReload = false;//清除更新标记(只有重新启动的时候需要去创建新的fragment对象),防止正常情况下频繁创建对象
}//end of if
return fragment;
}

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


以上是关于Fragment切换问题的主要内容,如果未能解决你的问题,请参考以下文章

如何使用导航组件切换到不同后堆栈中的其他片段?

从片段中的相机拍照

Commonsware CWAC 切换片段

使用 BottomNavigationView 在它们之间切换时保留片段状态

ViewPager开关没有显示片段之间

Android Reorder Fragment Backstack