当前位置:网站首页>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 .
边栏推荐
- This article describes the step-by-step process of starting the NFT platform project
- Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
- 跳出舒适区,5年点工转型自动化测试工程师,我只用了3个月时间
- 【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
- MD5 of Oracle
- Account management of MySQL
- Exchange rate query interface
- Custom classloader that breaks parental delegation
- Didi open source Delta: AI developers can easily train natural language models
- 高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
猜你喜欢

Lost a few hairs, and finally learned - graph traversal -dfs and BFS

高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
![[yolo3d]: real time detection of end-to-end 3D point cloud input](/img/5e/f17960d302f663db75ad82ae0fd70f.jpg)
[yolo3d]: real time detection of end-to-end 3D point cloud input

Kubernetes cluster storageclass persistent storage resource core concept and use

Gradle foundation | customize the plug-in and upload it to jitpack

【人员密度检测】基于形态学处理和GRNN网络的人员密度检测matlab仿真

0 foundation how to learn automated testing? Follow these seven steps step by step and you will succeed

Large screen visualization from bronze to the advanced king, you only need a "component reuse"!

Comment élaborer une stratégie nuageuse à l'ère des nuages mixtes

"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
随机推荐
Oracle 查看被锁的表和解锁
傅里叶级数
[untitled] basic operation of raspberry pie (2)
Detailed explanation of ThreadLocal
[database]jdbc
一文彻底理解评分卡开发中——Y的确定(Vintage分析、滚动率分析等)
[mv-3d] - multi view 3D target detection network
Knowing things by learning | self supervised learning helps improve the effect of content risk control
MySQL connection query and subquery
KL divergence is a valuable article
u本位合约爆仓清算解决方案建议
How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
Kotlin基础学习 16
[yolo3d]: real time detection of end-to-end 3D point cloud input
Pycharm2021 delete the package warehouse list you added
JIT deep analysis
Vite: configure IP access
Exchange rate query interface
Haute performance et faible puissance Cortex - A53 Core Board | i.mx8m mini
One of the future trends of SAP ui5: embrace typescript