总结
最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的14套腾讯、字节跳动、阿里、百度等2021最新面试真题解析,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
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关联的视图层次结构。
学习分享,共勉
Android高级架构师进阶之路
题外话,我在阿里工作多年,深知技术改革和创新的方向,Android开发以其美观、快速、高效、开放等优势迅速俘获人心,但很多Android兴趣爱好者所需的进阶学习资料确实不太系统,完整。今天我把我搜集和整理的这份学习资料分享给有需要的人
- Android进阶知识体系学习脑图
- Android进阶高级工程师学习全套手册
- 对标Android阿里P7,年薪50w+学习视频
- 大厂内部Android高频面试题,以及面试经历
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-wvhjhbog-1715734736398)]
- Android进阶高级工程师学习全套手册
[外链图片转存中…(img-CaWJE0nf-1715734736398)]
- 对标Android阿里P7,年薪50w+学习视频
[外链图片转存中…(img-sLFEziID-1715734736399)]
- 大厂内部Android高频面试题,以及面试经历
[外链图片转存中…(img-Ns8UNpY6-1715734736399)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!