当前位置:网站首页>Android kotlin fragment technology point
Android kotlin fragment technology point
2022-07-02 13:31:00 【lpf_ wei】
1.Fragment Definition
Fragment It's something that can be embedded in Activity In the middle of UI fragment , It allows programs to make more rational use of screen space , It can be understood as a mini Activity
2.Fragment Declaration period
onAttach()->onCreate()->onCreateView()->onActivityCreate()->onStart()->onResume()->onPause()->onStop()->onDestroyView()->onDestroy()->onDetach()
- onAttach(): When Fragment and Activity Call... When establishing an association
- onCreateView(): by Fragment When creating a view, call
- onActivityCreate(): Ensure that Fragment The associated Activity Callback after creation
- onDestroyView(): When and Fragment Called when the associated view is removed
- onDetach(): When Fragment and Activity Called when disassociated
3.Fragment Use
<fragment android:id="@+id/leftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.test.kotlin_test.fragment.LeftFragment"
/>
adopt fragment Labels are added directly to the layout file , adopt name To specify which fragment, This method is directly fixed in the layout file , If it needs to be replaced, it is not very convenient .
private fun replaceFragment(rightFragment: Fragment) {
val fragmentManager = supportFragmentManager
val transaction = fragmentManager.beginTransaction()
transaction.apply {
replace(R.id.rightFragment, rightFragment)
addToBackStack(null)
commit()
}
}
Dynamic replacement and addition Fragment, adopt addToBackStack Method can add transactions to the return stack , Click back to return to the previous one fragment The state of , You need to pass a name to describe the status of the return stack , According to the demand, if there are no special requirements, you can send null
4.Fragment and Activity Interaction
val fragmentActivity = activity as FragmentActivity
stay Fragment Pass through getActivity Method gets the containing Activity example , After forced rotation, it can be executed Activity The way inside
val leftFragment = supportFragmentManager.findFragmentById(R.id.leftFragment) as LeftFragment
stay Activity perform findFragmentById You can get the loaded fragment example , It can be easily executed fragment The way inside
5.Activity towards Fragment Pass parameters
// adopt Bundle Pass parameters , And put bundle Set to argument
val bundle=Bundle()
bundle.putString(LeftFragment.KEY_PARAM,"test")
rightFragment.arguments=bundle
// stay Fragment Of onCreate Method to get parameters
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val param = arguments?.getString(KEY_PARAM)
}
Fragment There is one argument Parameters , You can use this to give Fragment Pass parameters .
6. Dynamic loading layout
Previously, I learned about dynamic loading Fragment The way , The program can also load the layout file according to the resolution of the device or the size of the screen , To achieve this, you need to understand qualifiers
large qualifiers
newly build res below layout-large Folder , Create a new layout file with the same name under this file activity_fragment.xml, This one contains left and right Fragment,layout inside activity_fragment.xml Contains a Fragment.
Deploying the program to the flat-panel simulator shows layout-large The layout file inside
Deploying the program on the mobile phone shows layout The layout file inside
smallest-width Minimum width qualifier
Specify a minimum value for the width of the screen , Then take this minimum value as the critical value
You can specify different layout files under different width resolutions , Achieve perfect layout file adaptation
layout-sw500dp
layout-sw400dp
边栏推荐
- Unity SKFramework框架(二十)、VFX Lab 特效库
- [OpenGL] notes 29. Advanced lighting (specular highlights)
- leetcode621. task scheduler
- 日本赌国运:Web3.0 ,反正也不是第一次失败了!
- Principle analysis of security rememberme
- de4000h存储安装配置
- 记忆函数的性能优化
- Fundamentals of machine learning (II) -- division of training set and test set
- [true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
- Web Foundation
猜你喜欢
Unity skframework framework (XII), score scoring module
Node. JS accessing PostgreSQL database through ODBC
Everyone wants to eat a broken buffet. It's almost cold
解答:EasyDSS视频点播时音频是否可以设置为默认开启?
Unity SKFramework框架(十九)、POI 兴趣点/信息点
题解:《压缩技术》(原版、续集版)
Three methods of finding LCA of the nearest common ancestor
Unity skframework framework (XXI), texture filter map resource filtering tool
操作教程:EasyDSS如何将MP4点播文件转化成RTSP视频流?
【OpenGL】笔记二十九、高级光照(镜面高光)
随机推荐
Pocket Raider comments
Unity SKFramework框架(十九)、POI 兴趣点/信息点
[youcans' image processing learning course] general contents
Jerry's watch time synchronization [chapter]
Unity SKFramework框架(十五)、Singleton 单例
Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
[error record] cannot open "XXX" because Apple cannot check whether it contains malware
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
Redis database persistence
Quantum three body problem: Landau fall
JS逆向之巨量创意signature签名
Unity skframework framework (XV), singleton singleton
日本赌国运:Web3.0 ,反正也不是第一次失败了!
Fundamentals of machine learning (II) -- division of training set and test set
Explanation of 34 common terms on the Internet
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
Jerry's weather code table [chapter]
Unity SKFramework框架(十四)、Extension 扩展函数
Jerry's weather code table [chapter]
TVOC, VOC, VOCs gas detection + Solution