<Boolean> isShow1 = new MutableLiveData<>(); private final MutableLiveData<String> payState = new MutableLiveData<>(); private final MutableLiveData<String> opFare = new MutableLiveData<>(); private final MutableLiveData<String> discFare = new MutableLiveData<>(); //卡号 private final MutableLiveData<String> cardAsn = new MutableLiveData<>(); //账户余额 private final MutableLiveData public MutableLiveData<String> getCardAsn() { return cardAsn; } public MutableLiveData
3、LiveData与MutableLiveData区别 4、LiveData的使用 5、MutableLiveData的使用 6、LiveData和MutableLiveData的可变与不可变 7、 下面代码讲解 抽象类,无法直接new 2、什么是MutableLiveData MutableLiveData的父类是LiveData 用法和LiveData类似,也是在注册观察者回调里查看更新数据 可变 (下面会贴代码) MutableLiveData则是完全是整个实体类或者数据类型变化后才通知.不会细节到某个字段 4、LiveData的使用 public class UserBean extends public class MyViewModel extends ViewModel { private MutableLiveData<String> mStr; public MutableLiveData 总结: LiveData一般使用在实体类,MutableLiveData作用在变量上,他们通常和ViewModel结合使用,上面例子过于简单,工作当中可按照业务需要进行调整。
用户信息变更时触发订单异常刷新 源码定位(LiveDataBus.java): public class LiveDataBus { private final HashMap<String, MutableLiveData <Object>> eventMap = new HashMap<>(); public MutableLiveData<Object> with(String key) { return eventMap.getOrDefault(key, new MutableLiveData<>()); } } 污染原理:全局HashMap未做模块隔离,不同模块的同名 buildKey(module, domain, event) return busMap.getOrPut(key) { SafeMutableLiveData<T>() } as MutableLiveData 版本号屏障:继承MutableLiveData重写observe方法,添加版本校验逻辑 2. 事件生命周期:给每个事件绑定发布者的Context,跟随发布模块生命周期自动清理 3.
对象,它是 LiveData 的子类,然后给它添加观察者对象,代码如下: java: final MutableLiveData<String> simpleLiveData = new MutableLiveData 前面介绍 Transformations 的变换操作实际上就是返回的 MediatorLiveData ,看一下 MediatorLiveData 的使用: java: MutableLiveData <User> userLiveData1 = new MutableLiveData<>(); MutableLiveData<User> userLiveData2 = new MutableLiveData <User>() val userLiveData2 = MutableLiveData<User>() val userLiveData = MutableLiveData<User>() userLiveData1 <User> userLiveData = new MutableLiveData<>(); public void loadUser(){ //...
android.arch.lifecycle.AndroidViewModel; import android.arch.lifecycle.LiveData; import android.arch.lifecycle.MutableLiveData filename of the PDF. */ private static final String FILENAME = "sample.pdf"; private final MutableLiveData <PageInfo> mPageInfo = new MutableLiveData<>(); private final MutableLiveData<Bitmap> mPageBitmap = new MutableLiveData<>(); private final MutableLiveData<Boolean> mPreviousEnabled = new MutableLiveData <>(); private final MutableLiveData<Boolean> mNextEnabled = new MutableLiveData<>(); private
<UserData> liveData = new MutableLiveData<>(); liveData.observe(this, new Observer<UserData>( MutableLiveData:可变的LiveData 普通的LiveData将setValue以及postValue的作用于定义成protected,除非继承LiveData否则访问这两个函数。 而MutableLiveData只是Override这两个函数,将protected作用域扩大成了public,以至于外部可以访问。 MutableLiveData<UserData> localCache = new MutableLiveData<>(); localCache.observe(this, userData -> { // TODO: }); MutableLiveData<UserData> networkData = new MutableLiveData<>(); networkData.observe
<Integer> mCount = new MutableLiveData<>(); public void add() { mCount.setValue(mCount.getValue <Integer> mCount = new MutableLiveData<>(); public void add() { mCount.postValue(mCount.getValue <Student> studentMutableLiveData = new MutableLiveData<>(); public void setStudentMutableLiveData <Student> studentMutableLiveData = new MutableLiveData<>(); Student student = new Student(); <Integer> score = new MutableLiveData<>(); public void setScore(int score) { this.score.setValue
import androidx.lifecycle.ViewModel class MyViewModel: ViewModel() { lateinit var number: MutableLiveData <Int> init { number = MutableLiveData() number.value = 0 } } 然后 , 在 Activity 成员 ; import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel class MyViewModel : ViewModel() { lateinit var number: MutableLiveData<Int> init { number = MutableLiveData <Int> init { number = MutableLiveData() number.value = 0 } fun plus():
class JokesDetailViewModel : ViewModel() { //创建 LiveData private val _state by lazy { MutableLiveData 就有了 MutableLiveData 继承 LiveData 将发送数据的方法改为了 public public class MutableLiveData<T> extends LiveData<T > { /** * Creates a MutableLiveData initialized with the given {@code value} * * @param value initial value */ public MutableLiveData(T value) { super (value); } /** * Creates a MutableLiveData with no value assigned to it. */ public
// 别这么做 public class MyViewModel extends AndroidViewModel { public final MutableLiveData<String> statusLabel = new MutableLiveData<>(); public SampleViewModel(Application context) { // 显示资源ID public class MyViewModel extends ViewModel { public final MutableLiveData<Int> statusLabel = new MutableLiveData<>(); public SampleViewModel(Application context) { super(context
<T> , class MyViewModel: ViewModel { var second: MutableLiveData<Int> = MutableLiveData<Int>() import androidx.lifecycle.ViewModel class MyViewModel: ViewModel { var second: MutableLiveData< Int> = MutableLiveData<Int>() constructor() { second.value = 0 } } 2、Activity 组件代码 在 import androidx.lifecycle.ViewModel class MyViewModel: ViewModel { var progress: MutableLiveData <Int> = MutableLiveData<Int>() constructor() { progress.value = 0 } } 2、Activity 组件代码
// 别这么做 public class MyViewModel extends AndroidViewModel { public final MutableLiveData<String> statusLabel = new MutableLiveData<>(); public SampleViewModel(Application context) { // 显示资源ID public class MyViewModel extends ViewModel { public final MutableLiveData<Int> statusLabel = new MutableLiveData<>(); public SampleViewModel(Application context) { super(context
; /** * Created by aruba on 2021/9/11. */ public class Idol { private MutableLiveData<String> name; private MutableLiveData<Integer> star; private MutableLiveData<String> imageUrl; private MutableLiveData<Integer> score; public Idol(String name, int star, String imageUrl,int score) { MutableLiveData<>(); this.star.setValue(star); this.imageUrl = new MutableLiveData<> MutableLiveData<Integer> getStar() { return star; } public MutableLiveData<String>
在实际使用中,用得比较多的是 MutableLiveData。他常常结合 ViewModel 一起使用。下面,让我们一起来看一下怎样使用它? public class TestViewModel extends ViewModel { private MutableLiveData<String> mNameEvent = new MutableLiveData<>(); public MutableLiveData<String> getNameEvent() { return mNameEvent; mTestViewModel = ViewModelProviders.of(this).get(TestViewModel.class); MutableLiveData<String> nameEvent <String> mNameEvent = new MutableLiveData<>(); public MutableLiveData<String> getNameEvent() {
public class NameViewModel extends ViewModel { private MutableLiveData<String> name; public MutableLiveData<String> getName() { if (name == null) { name = new MutableLiveData 事实上,最简单的LiveData使用方法是MutableLiveData,如下所示。 <String> mutableLiveData = new MutableLiveData<>(); mutableLiveData.observe(this, new Observer 通常情况下会在 ViewModel 中使用 MutableLiveData,然后 ViewModel 只会向观察者公开不可变的 LiveData 对象,如下所示。
三.LiveData的基本使用方法 LiveData是一个抽象类,不能直接使用,通常使用的是他的直接子类MutableLiveData。 下面我们改造上一节的代码,如下所示: public class TimerViewModel extends ViewModel { private MutableLiveData<Integer LiveData<Integer> getCurrentSecond(){ if(currentSecond==null){ currentSecond=new MutableLiveData TimerViewModel timerViewModel=new ViewModelProvider(this).get(TimerViewModel.class); MutableLiveData <Integer> liveData= (MutableLiveData<Integer>) timerViewModel.getCurrentSecond(); liveData.observe
可以订阅数据,TokensViewModel类像下面,代码[3]有删减: public class TokensViewModel extends ViewModel { private final MutableLiveData <ETHWallet> defaultWallet; private final MutableLiveData<NetworkInfo> defaultNetwork; private final MutableLiveData<Token[]> tokens; private final MutableLiveData<Ticker> prices; } MutableLiveData 显示当前账号 可以分为两个步骤: •从数据库中读取账号;•界面显示账号 TokensViewModel中定义了一个MutableLiveData<ETHWallet> defaultWallet ,从数据库中读取账号会保存在
2.用法 (1)创建LiveData对象 public class UserModel extends ViewModel { private MutableLiveData<String> name ; int age; public MutableLiveData<String> getName() { if (name == null) { name = new MutableLiveData<>(); name.setValue("lalalala"); } return name;
<String> msg; private MutableLiveData<Integer> count; private MutableLiveData<List<ImageInfo >> data; public MutableLiveData<Integer> getCount() { if (count == null) { count = new MutableLiveData<>(); } return count; } public MutableLiveData<String> msg; } public MutableLiveData<List<ImageInfo>> getData() { if(data == null){ data = new MutableLiveData<>(); } return data; } } 4.MainActivity.java public class
我们首先定义一个极简的ViewModel: public class TestViewModel extends ViewModel { private MutableLiveData<String > currentName; public MutableLiveData<String> getCurrentName() { if (currentName == null) { currentName = new MutableLiveData<String>(); } return currentName; 看下Kotlin中 lambda的写法: fun test2() { val liveData = MutableLiveData<Int>() for (i in 0..9) liveData = new MutableLiveData(); int i = 0; do { int i2 = i; i+