加载单个fragment效果 头部标题多个fragment
底部标题按钮多个fragment 底部标题按钮多个fragment
FragmentPagerAdapter实现,会重复onCreateView FragmentManager实现。不会重复onCreateView
itemview按钮,viewpager加载fragment 固定按钮,FrameLayout加载fragment
实现方法:
1、加载单个fragment
framelayout加载fragment
1、activity的layout文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"android:layout_height="match_parent"><FrameLayoutandroid: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;@Overridepublic 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() {@Overridepublic 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;
、、、、、、、、、、
}
3、FragmentPagerAdapter实现viewpager加载多个fragment
public class MyFragmentAdapter extends FragmentPagerAdapter {private List<Fragment> fragmentList;private List<String> titleList;private Context context;public MyFragmentAdapter(FragmentManager fragmentManager, List<Fragment> fragmentList, List<String> titleList,Context context) {super(fragmentManager);this.fragmentList = fragmentList;this.titleList = titleList;this.context = context;}@Overridepublic Fragment getItem(int position) {return fragmentList.get(position);}@Overridepublic int getCount() {return titleList.size();}//注意!!!这里就是我们自定义的布局tab_itempublic View getCustomView(int position){View view= LayoutInflater.from(context).inflate(R.layout.tab_item,null);ImageView iv= (ImageView) view.findViewById(R.id.tab_iv);TextView tv= (TextView) view.findViewById(R.id.tab_tv);switch (position){case 0://drawable代码在文章最后贴出iv.setImageDrawable(context.getResources().getDrawable(R.drawable.home_icon_selector));tv.setText("首页");break;case 1:iv.setImageDrawable(context.getResources().getDrawable(R.drawable.txl_icon_selector));tv.setText("通讯录");break;case 2:iv.setImageDrawable(context.getResources().getDrawable(R.drawable.wd_icon_selector));tv.setText("我的");break;}return view;}
}
demo云盘链接:https://pan.baidu.com/s/1FWbP938mW0NB_92WJjHQQA
云盘密码交流:QQ1085220040
demo链接:https://download.csdn.net/download/meixi_android/11225062
资讯fragment实现及资讯频道管理:https://blog.csdn.net/meixi_android/article/details/90715243