当前位置:网站首页>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 .
边栏推荐
- [personal notes] PHP common functions - custom functions
- [designmode] Prototype Pattern
- 毕设-基于SSM电影院购票系统
- 【DesignMode】原型模式(prototype pattern)
- Pointer array & array pointer
- 近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
- 知物由学 | 自监督学习助力内容风控效果提升
- JIT deep analysis
- The 10th Blue Bridge Cup single chip microcomputer provincial competition
- 蓝桥杯单片机省赛第十二届第二场
猜你喜欢

蓝桥杯单片机省赛第十一届

NLog使用

MySQL之账号管理

Analyse de 43 cas de réseaux neuronaux MATLAB: Chapitre 42 opérations parallèles et réseaux neuronaux - - opérations parallèles de réseaux neuronaux basées sur CPU / GPU

初识string+简单用法(二)

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

Didi open source Delta: AI developers can easily train natural language models

The 8th Blue Bridge Cup single chip microcomputer provincial competition

Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud

汇率的查询接口
随机推荐
ImageAI安装
MySQL之账号管理
Oracle 常用SQL
Blue Bridge Cup SCM digital tube skills
焱融看 | 混合雲時代下,如何制定多雲策略
Kotlin basic learning 14
The 6th Blue Bridge Cup single chip microcomputer provincial competition
[punch in] flip the string (simple)
Custom classloader that breaks parental delegation
What is the logical structure of database file
Flutter中深入了解MaterialApp,常用属性解析
[golang] leetcode intermediate bracket generation & Full Permutation
Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
Basic syntax of unity script (6) - specific folder
Influence of air resistance on the trajectory of table tennis
蓝桥杯单片机省赛第十一届
h5中的页面显示隐藏执行事件
The first game of the 12th Blue Bridge Cup single chip microcomputer provincial competition
UI (New ui:: MainWindow) troubleshooting
The 5th Blue Bridge Cup single chip microcomputer provincial competition