当前位置:网站首页>Jetpack's livedata extension mediatorlivedata
Jetpack's livedata extension mediatorlivedata
2022-07-02 03:41:00 【Jason_ Lee155】
LiveData The use of has already been mentioned :
Android Jetpack Of components LiveData Use - Source code
however Android There are also some extension classes in the Library , such as MediatorLiveData Need to introduce .
Suppose there is a need : stay ExitText While typing text in , Number of display text . Yes, of course EditTextChangeListener, Or just one LiveData Monitor input String You can also get length, But here are just examples .
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 The role of , As the name suggests, it can act as an intermediary to monitor others LiveData. It can be here EditText Through postMessage to update message,count adopt addSource monitor message Update the input length after the change of .
use Java Look at the version :
for example : from getNameFromServer() The value taken is "alan", and MediatorLiveData After transformation, it becomes "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);
}
});
}
}
As for the principle , I believe I can use LiveData I think it's nothing ,studio Just click this class to enter the source code and have a look .
边栏推荐
- Oracle common SQL
- The 8th Blue Bridge Cup single chip microcomputer provincial competition
- h5中的页面显示隐藏执行事件
- How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
- The 6th Blue Bridge Cup single chip microcomputer provincial competition
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验2 蜂鸣器实验(学习笔记)
- Pointer array & array pointer
- 蓝桥杯单片机省赛第九届
- MySQL connection query and subquery
- ThreadLocal详解
猜你喜欢
0基础如何学习自动化测试?按照这7步一步一步来学习就成功了
Eight steps of agile development process
Set vscode. When double clicking, the selected string includes the $symbol - convenient for PHP operation
蓝桥杯单片机省赛第六届
微信小程序中 在xwml 中使用外部引入的 js进行判断计算
JIT deep analysis
【DesignMode】建造者模式(Builder model)
Kubernetes cluster storageclass persistent storage resource core concept and use
蓝桥杯单片机省赛第十一届
What do you know about stock selling skills and principles
随机推荐
Didi open source Delta: AI developers can easily train natural language models
蓝桥杯单片机省赛第十二届第二场
In depth interpretation of pytest official documents (26) customized pytest assertion error information
NLog use
Merge interval, linked list, array
蓝桥杯单片机省赛第十一届
Qt的网络连接方式
Lost a few hairs, and finally learned - graph traversal -dfs and BFS
蓝桥杯单片机第四届省赛
PY3 link MySQL
Retrofit's callback hell is really vulnerable in kotlin synergy mode
软件测试人的第一个实战项目:web端(视频教程+文档+用例库)
u本位合约爆仓清算解决方案建议
[golang] leetcode intermediate bracket generation & Full Permutation
Vite: configure IP access
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
FFMpeg AVFrame 的概念.
蓝桥杯单片机省赛第七届
Kotlin basic learning 14
VS2010 plug-in nuget