当前位置:网站首页>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里点这个类进入源码看看就行了。
边栏推荐
- Oracle viewing locked tables and unlocking
- Discrimination between sap Hana, s/4hana and SAP BTP
- The fourth provincial competition of Bluebridge cup single chip microcomputer
- 蓝桥杯单片机省赛第十一届第二场
- The 10th Blue Bridge Cup single chip microcomputer provincial competition
- Global and Chinese market of X-ray detectors 2022-2028: Research Report on technology, participants, trends, market size and share
- [punch in] flip the string (simple)
- 蓝桥杯单片机省赛第五届
- Kotlin基础学习 17
- Failed to upgrade schema, error: “file does not exist
猜你喜欢

【DesignMode】原型模式(prototype pattern)

The 9th Blue Bridge Cup single chip microcomputer provincial competition

The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition

【DesignMode】建造者模式(Builder model)

Knowing things by learning | self supervised learning helps improve the effect of content risk control

Failed to upgrade schema, error: “file does not exist

近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下

The first game of the 11th provincial single chip microcomputer competition of the Blue Bridge Cup

Introduction to Robotics II. Forward kinematics, MDH method
![[HCIA continuous update] overview of dynamic routing protocol](/img/03/83c883afb63b7c63f6879b5513bac3.jpg)
[HCIA continuous update] overview of dynamic routing protocol
随机推荐
How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
MD5 of Oracle
Global and Chinese market of X-ray detectors 2022-2028: Research Report on technology, participants, trends, market size and share
Influence of air resistance on the trajectory of table tennis
Blue Bridge Cup single chip microcomputer sixth temperature recorder
5G时代全面到来,浅谈移动通信的前世今生
The fourth provincial competition of Bluebridge cup single chip microcomputer
MySQL connection query and subquery
Global and Chinese markets for infant care equipment, 2022-2028: Research Report on technology, participants, trends, market size and share
焱融看 | 混合雲時代下,如何制定多雲策略
js生成随机数
蓝桥杯单片机省赛第十届
What do you know about stock selling skills and principles
[database]jdbc
Kotlin basic learning 17
[designmode] Prototype Pattern
蓝桥杯单片机第四届省赛
Discrimination between sap Hana, s/4hana and SAP BTP
傅里叶级数
[HCIA continuous update] working principle of OSPF Protocol