首先要在app的build.gradle
加入:
dependencies {
implementation("androidx.fragment:fragment-ktx:1.3.6")
}
@MainThread inline fun <reified VM : ViewModel> Fragment.viewModels(
noinline ownerProducer: () -> ViewModelStoreOwner = { this },
noinline factoryProducer: () -> ViewModelProvider.Factory = null
): Lazy<VM>
这个套件可以使我们直接透过
by activityViewModels()
或by viewModels()
取用ViewModel
。
by activityViewModels()
:取得fragment
所在的activity
的viewModel
class MyFragment : Fragment() {
val viewmodel: MyViewModel by activityViewModels()
//或者这样写
//val viewmodel: MyViewModel by activityViewModels()
}
by viewModels()
:取得fragment
的viewModel
class MyFragment : Fragment() {
val viewmodel: MyViewModel by viewModels()
}
fragment
间的页面切换也可以从以往的:
supportFragmentManager.beginTransaction()
.addToBackStack("...")
.setCustomAnimations( R.anim.enter_anim, R.anim.exit_anim)
.replace(
R.id.fragment_container,
myFragment,
FRAGMENT_TAG
)
.commit()
改写为:
supportFragmentManager.commit {
addToBackStack("...")
setCustomAnimations( R.anim.enter_anim, R.anim.exit_anim)
replace(
R.id.fragment_container,
myFragment,
FRAGMENT_TAG
)
}
在这边只简单列出几个常用的方法,
如果有兴趣研究更多可以看看官网喔。
在资料库中除了有数字和字母之外,当然也会有字串,如果想要搜寻字串,就要使用'单引号' 而字串要使用运...
今天我们来看一个简单的程序: #include<stdio.h> int main(){...
.**电子密码本(ECB)**接受纯文本作为输入。 .**密码块链接(CBC)**接受“纯文本XOR...
最近都在全台跑面试 都没时间继续写.. 刚好面试某金控 面试官出了个回家作业给我 就花了一个下午把它...
前言: 前两遍的基础activity 和 fragment 就可以作出不错的app了,但功能愈来愈多...