android:background=“@color/color_188FFF”
android:gravity=“center”
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“我的”
android:textColor=“@color/white”
android:textSize=“@dimen/text_size_20”/>
2.自定义Fragment类,继承Fragment类或其子类,同时实现onCreateView()方法,在方法中通过inflater.inflate加载布局文件,接着返回其View
public class HomeFragment extends Fragment {
private TextView tv_on;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container,false);
MLog.e(this.getClass().getName()+“onCreateView”);
tv_on = view.findViewById(R.id.tv_on);
tv_on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MLog.e(this.getClass().getName()+“准备关闭”);
}
});
return view;
}
}
3.在需要加载Fragment的Activity对应布局文件中的name属性设为全限定类名,即包名.fragment.HomeFragment
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:id=“@+id/ll_bg”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:background=“@color/color_666666”
android:orientation=“horizontal”>
<fragment
android:id=“@+id/fragmen_home”
android:name=“com.scc.demo.fragment.HomeFragment”
android:layout_width=“0dp”
android:layout_weight=“1”
android:layout_height=“match_parent” />
<fragment
android:id=“@+id/fragmen_mine”
android:name=“com.scc.demo.fragment.MineFragment”
android:layout_width=“0dp”
android:layout_weight=“1”
android:layout_height=“match_parent” />
4.最后在Activity调用setContentView()加载布局文件即可
public class FragmentActivity extends ActivityBase {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MLog.e(this.getClass().getName()+“onCreate”);
setContentView(R.layout.activity_fragment);
}
}
动态创建
-
获得FragmentManager对象,通过getSupportFragmentManager()
-
获得FragmentTransaction对象,通过fm.beginTransaction()
-
调用add()方法或者repalce()方法加载Fragment;
-
最后调用commit()方法提交事务
样例:
1.activity的布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”>
<FrameLayout
android:id=“@+id/fl_frame”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_above=“@+id/rg_bottom_tab”/>
<RadioGroup
android:id=“@+id/rg_bottom_tab”
android:layout_width=“match_parent”
android:layout_height=“56dp”
android:layout_alignParentBottom=“true”
android:background=“#dcdcdc”
android:orientation=“horizontal”>
<RadioButton
android:id=“@+id/rb_home”
android:layout_width=“0dp”
android:layout_height=“match_parent”
android:layout_weight=“1”
android:button=“@null”
android:checked=“true”
android:gravity=“center”
android:text=“首页”
android:textSize=“@dimen/text_size_18” />
<RadioButton
android:id=“@+id/rb_list”
android:layout_width=“0dp”
android:layout_height=“match_parent”
android:layout_weight=“1”
android:button=“@null”
android:checked=“true”
android:gravity=“center”
android:text=“列表”
android:textSize=“@dimen/text_size_18” />
<RadioButton
android:id=“@+id/rb_news”
android:layout_width=“0dp”
android:layout_height=“match_parent”
android:layout_weight=“1”
android:button=“@null”
android:checked=“true”
android:gravity=“center”
android:text=“消息”
android:textSize=“@dimen/text_size_18” />
<RadioButton
android:id=“@+id/rb_mine”
android:layout_width=“0dp”
android:layout_height=“match_parent”
android:layout_weight=“1”
android:button=“@null”
android:gravity=“center”
android:text=“我的”
android:textSize=“@dimen/text_size_18” />
2.自定义Fragment
public class HomeFragment extends Fragment {
private TextView tv_on;
public static HomeFragment newInstance(String param){
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(“param”, param);
fragment.setArguments(args);
return fragment;
}
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container,false);
MLog.e(this.getClass().getName()+“onCreateView”);
Bundle bundle = getArguments();
String param = bundle.getString(“param”);
tv_on = view.findViewById(R.id.tv_on);
tv_on.setText(“首页”+param);
tv_on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MLog.e(this.getClass().getName()+“准备关闭”);
}
});
return view;
}
}
3.获得FragmentManager对象,通过getFragmentManager()
public class FragmentActivity extends ActivityBase {
private RadioGroup rg_bottom_tab;
private SparseArray mFragmentList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
MLog.e(this.getClass().getName()+“onCreate”);
initView();
}
public void initView(){
rg_bottom_tab = findViewById(R.id.rg_bottom_tab);
mFragmentList = new SparseArray<>();
mFragmentList.append(R.id.rb_home, HomeFragment.newInstance(“我最帅”));
mFragmentList.append(R.id.rb_list, ListFragment.newInstance(“我最美”));
mFragmentList.append(R.id.rb_news, NewsFragment.newInstance(“我最新”));
mFragmentList.append(R.id.rb_mine, MineFragment.newInstance(“这里是我的”));
rg_bottom_tab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 具体的fragment切换逻辑可以根据应用调整,例如使用show()/hide()
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fl_frame, mFragmentList.
get(checkedId));
transaction.commit();
}
});
// 默认显示第一个
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.fl_frame, mFragmentList.get(R.id.rb_home)).commit();
}
}
四、FragmentPageAdapter和FragmentPageStateAdapter的区别
FragmentPageAdapter在每次切换页面的的时候,是将Fragment进行分离,适合页面较少的Fragment使用以保存一些内存,对系统内存不会多大影响。
FragmentPageStateAdapter在每次切换页面的时候,是将Fragment进行回收,适合页面较多的Fragment使用,这样就不会消耗更多的内存。
五、Fragment生命周期
被调用以使Fragment****恢复到恢复状态(与用户交互)****的核心生命周期方法系列是:
onAttach(Activity) 一旦Fragment与其活动相关联,就会调用。
onCreate(Bundle) 调用以进行Fragment的初始创建。
onCreateView(LayoutInflater, ViewGroup, Bundle) 创建并返回与Fragment关联的视图层次结构。
****onActivityCreated(Bundle)****告诉Fragment它的活动已经完成了它自己的Activity.onCreate().
onViewStateRestored(Bundle) 告诉Fragment其视图层次结构的所有保存状态都已恢复。
onStart() 使Fragment对用户可见(基于正在启动的包含活动)。
onResume() 使Fragment开始与用户交互(基于其包含的活动正在恢复)。
由于不再使用********Fragment,它会经历一系列反向回调:
onPause() Fragment不再与用户交互,因为其活动被暂停或片段操作正在活动中修改它。
onStop() Fragment不再对用户可见,因为其活动正在停止或片段操作正在活动中修改它。
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
最后
如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。
欢迎大家一起交流讨论啊~
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
v7q14tVD-1712366211764)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
最后
如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。
[外链图片转存中…(img-QM6uQbgT-1712366211765)]
欢迎大家一起交流讨论啊~
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!