当前位置:网站首页>Jetpack之LiveData扩展MediatorLiveData
Jetpack之LiveData扩展MediatorLiveData
2022-07-02 03:38:00 【Jason_Lee155】
LiveData的使用前面已经说过:
Android Jetpack组件之 LiveData使用-源码
但是Android库里也有一些扩展类,比如 MediatorLiveData 需要介绍下。
假设有一个需求:在ExitText中输入文字的同时,显示文字个数。当然可以用EditTextChangeListener,也可以只用一个LiveData监听输入String也可以得到length,但是这里只是举例嘛。
class MainViewModel : ViewModel() {
val message: MutableLiveData<String> = MutableLiveData()
val count: MediatorLiveData<Int> = MediatorLiveData()
init {
count.value = 0
count.addSource(message) {
val cnt = message.value?.length ?: 0
count.postValue(cnt)
}
}
fun postMessage(message: String) {
this.message.postValue(message)
}
}
MediatorLiveDarta的作用,顾名思义可以作为中间人的角色监听其他LiveData。这里可以在EditText的回调里通过postMessage更新message,count通过addSource监听message的变化后更新输入长度。
用Java版本看看:
例如:从getNameFromServer()取的值是"alan",而MediatorLiveData做了转化后成了"alan gong"。
public class MyViewModel extends ViewModel {
private MutableLiveData<String> liveEvent;
private MediatorLiveData<String> testLiveData;
public MyViewModel() {
testLiveData = new MediatorLiveData<>();
testLiveData.addSource(liveEvent, new Observer<String>() {
@Override
public void onChanged(String s) {
testLiveData.postValue(s + " gong");
}
});
getNameFromServer();
}
public MediatorLiveData<String> getTestLiveData() {
return testLiveData;
}
private void getNameFromServer() {
if (liveEvent == null) {
liveEvent = new MutableLiveData<>();
}
liveEvent.setValue("alan");
}
}
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(LayoutInflater.from(this));
setContentView(binding.getRoot());
ViewModelProvider provider = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory());
MyViewModel myViewModel = provider.get(MyViewModel.class);
binding.setViewModel(myViewModel);
myViewModel.getTestLiveData().observe(this, new Observer<String>() {
@Override
public void onChanged(String s) {
textView.setText(s);
}
});
}
}
至于原理,相信会使用LiveData的都觉得没什么,studio里点这个类进入源码看看就行了。
边栏推荐
- Kotlin 基础学习13
- 滴滴开源DELTA:AI开发者可轻松训练自然语言模型
- Generate random numbers that obey normal distribution
- 近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
- aaaaaaaaaaaaa
- 毕设-基于SSM电影院购票系统
- Kotlin basic learning 17
- The page in H5 shows hidden execution events
- Merge interval, linked list, array
- The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
猜你喜欢
焱融看 | 混合雲時代下,如何制定多雲策略
NLog use
Knowing things by learning | self supervised learning helps improve the effect of content risk control
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
u本位合约爆仓清算解决方案建议
蓝桥杯单片机第四届省赛
Flutter中深入了解MaterialApp,常用属性解析
Eight steps of agile development process
Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)
随机推荐
This article describes the step-by-step process of starting the NFT platform project
蓝桥杯单片机省赛第九届
PY3 link MySQL
h5中的页面显示隐藏执行事件
[HCIA continuous update] overview of dynamic routing protocol
How about Ping An lifetime cancer insurance?
高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
焱融看 | 混合云时代下,如何制定多云策略
Unity脚本的基础语法(7)-成员变量和实例化
Oracle viewing locked tables and unlocking
Basic syntax of unity script (6) - specific folder
One of the future trends of SAP ui5: embrace typescript
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
The second game of the 12th provincial single chip microcomputer competition of the Blue Bridge Cup
JS generate random numbers
[punch in] flip the string (simple)
VS2010 plug-in nuget
In depth interpretation of pytest official documents (26) customized pytest assertion error information
一天上手Aurora 8B/10B IP核(5)----从Framing接口的官方例程学起
【小技巧】使用matlab GUI以对话框模式读取文件