1、activity的layout文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/onef"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</android.support.constraint.ConstraintLayout>/3
2、创建fragment
public class Fragment1 extends Fragment {
public static Fragment1 getInstance(Bundle bundle) {
Fragment1 fg = new Fragment1();
fg.setArguments(bundle);
return fg;
}
protected View contentView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
contentView = inflater.inflate(R.layout.fragment1, container, false);
initViews();
return contentView;
}
public void initViews(){
TextView textView =(TextView)contentView.findViewById(R.id.f1te);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(),"ssssf1",Toast.LENGTH_SHORT).show();
}
});
}
}
3、加载fragment
public void switchContent() {
//必需继承FragmentActivity,嵌套fragment只需要这行代码
getSupportFragmentManager().beginTransaction().replace(R.id.onef, new Fragment2()).commitAllowingStateLoss();
}
同一布局,显示不同fragment内容
private GridDemoFragment f\_a;
private ListDemoFragment f\_b;
private Fragment\[\] mFragments;
private int mIndex;
//方法一,默认第一fragment
private void initFragment() {
f\_a = new GridDemoFragment();
f\_b = new ListDemoFragment();
//添加到数组
mFragments = new Fragment\[\]{f\_a, f\_b};
//开启事务
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
//添加首页
ft.add(R.id.content, f\_a).commit();
//默认设置为第0个
setIndexSelected(0);
}
//方法一,选中显示与隐藏
private void setIndexSelected(int index) {
if (mIndex == index) {
return;
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
//隐藏
ft.hide(mFragments\[mIndex\]);
//判断是否添加
if (!mFragments\[index\].isAdded()) {
ft.add(R.id.content, mFragments\[index\]).show(mFragments\[index\]);
} else {
ft.show(mFragments\[index\]);
}
ft.commit();
//再次赋值
mIndex = index;
}
//方法二,选中替换
private void setIndexSelectedTwo(int index) {
switch (index) {
case 0:
changeFragment(new GridDemoFragment().getFragmentGridDemoFragment());
break;
case 1:
changeFragment(new ListDemoFragment().getFragmentListDemoFragment());
break;
default:
break;
}
}
方法二,默认第一fragment
private void changeFragment(Fragment fm) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = supportFragmentManager.beginTransaction();
transaction.replace(R.id.content, fm);
transaction.commit();
}
// 单例(方法二)
private static GridDemoFragment fa;
public static GridDemoFragment getFragmentGridDemoFragment() {
if (fa == null) {
fa = new GridDemoFragment();
}
return fa;
}
2、FragmentManager实现framelayout加载多个fragment
private Fragment mCurrentFragment;
private FragmentManager fragmentManager;
private SparseArray<Fragment> mainFragments;
mainFragments = new SparseArray<>();
fragmentManager = getSupportFragmentManager();
switchFragment(R.id.rl_home);
public void switchFragment(int resId) {
Fragment fg;
if (mainFragments.indexOfKey(resId) > -1) {
fg = mainFragments.get(resId);
} else {
switch (resId) {
case R.id.rl_home:
mainFragments.put(resId, Fragment1.getInstance(null));
break;
case R.id.rl_lishi:
mainFragments.put(resId, Fragment2.getInstance(null));
break;
case R.id.rl_me:
mainFragments.put(resId, Fragment3.getInstance(null));
break;
}
fg = mainFragments.get(resId);
}
switchContent(fg);
}
/**
* 切换数据
*/
private void switchContent(Fragment fg) {
if (fg == null || mCurrentFragment == fg)
return;
if (mCurrentFragment != null && mCurrentFragment != fg)
getSupportFragmentManager().beginTransaction().hide(mCurrentFragment).commitAllowingStateLoss();
mCurrentFragment = fg;
、、、、、、、、、、
}