当前位置:网站首页>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
边栏推荐
- How much do you know about free SSL certificates? The difference between free SSL certificate and charged SSL certificate
- OpenAPI generator: simplify the restful API development process
- 日本赌国运:Web3.0 ,反正也不是第一次失败了!
- [Unity]使用GB2312,打包后程序不正常解决方案
- Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking
- Error function ERF
- Verification failed, please check your call back website. You can follow the instructions
- 国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)
- numpy数组计算
- I did it with two lines of code. As a result, my sister had a more ingenious way
猜你喜欢

MAC (MacOS Monterey 12.2 M1) personal use PHP development

题解《子数整数》、《欢乐地跳》、《开灯》

Explanation: here is your UFO, Goldbach conjecture

2、 Frame mode MPLS operation

Unity SKFramework框架(十二)、Score 计分模块

OpenFOAM:lduMatrix&lduAddressing

Japan bet on national luck: Web3.0, anyway, is not the first time to fail!

Unity SKFramework框架(十六)、Package Manager 开发工具包管理器

Can automatically update the universal weekly report template, you can use it with your hand!

Three methods of finding LCA of the nearest common ancestor
随机推荐
JS reverse massive creative signature
[OpenGL] notes 29. Advanced lighting (specular highlights)
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
[error record] cannot open "XXX" because Apple cannot check whether it contains malware
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
Node.js通过ODBC访问PostgreSQL数据库
Unity skframework framework (XIII), question module
不会看器件手册的工程师不是个好厨子
自主可控三维云CAD:CrownCAD赋能企业创新设计
Can automatically update the universal weekly report template, you can use it with your hand!
What are eNB, EPC and PGW?
Post order traversal sequence of 24 binary search tree of sword finger offer
How much do you know about free SSL certificates? The difference between free SSL certificate and charged SSL certificate
为什么switch 的default后面要跟break?
Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
Partner cloud form strong upgrade! Pro version, more extraordinary!
Node. JS accessing PostgreSQL database through ODBC
Download files and preview pictures
Verification failed, please check your call back website. You can follow the instructions
Unity SKFramework框架(二十)、VFX Lab 特效库