70 lines
2.1 KiB
Java
70 lines
2.1 KiB
Java
package com.xscm.moduleutil.widget;
|
|
|
|
import androidx.lifecycle.LiveData;
|
|
import androidx.lifecycle.MutableLiveData;
|
|
import androidx.lifecycle.ViewModel;
|
|
|
|
import com.xscm.moduleutil.bean.room.RoomInfoResp;
|
|
import com.xscm.moduleutil.enumType.QXRoomSeatViewType;
|
|
|
|
// 在 common 模块中或相应模块中
|
|
public class SharedViewModel extends ViewModel {
|
|
// 给roomFragment传递数据
|
|
private final MutableLiveData<RoomInfoResp> dataForFragment = new MutableLiveData<>();
|
|
private final MutableLiveData<Boolean> fragmentReady = new MutableLiveData<>(false);
|
|
private MutableLiveData<QXRoomSeatViewType> seatViewTypeData = new MutableLiveData<>();
|
|
//给子fragment传递数据
|
|
private MutableLiveData<RoomInfoResp> childFragmentData = new MutableLiveData<>();
|
|
|
|
// 为子Fragment设置数据的方法
|
|
public void setChildFragmentData(RoomInfoResp data) {
|
|
childFragmentData.setValue(data);
|
|
}
|
|
|
|
// 获取子Fragment数据的LiveData
|
|
public LiveData<RoomInfoResp> getChildFragmentData() {
|
|
return childFragmentData;
|
|
}
|
|
public void setSeatViewType(QXRoomSeatViewType type) {
|
|
seatViewTypeData.setValue(type);
|
|
}
|
|
|
|
public LiveData<QXRoomSeatViewType> getSeatViewType() {
|
|
return seatViewTypeData;
|
|
}
|
|
public LiveData<RoomInfoResp> getDataForFragment() {
|
|
return dataForFragment;
|
|
}
|
|
|
|
public void setDataForFragment(RoomInfoResp data) {
|
|
dataForFragment.setValue(data);
|
|
}
|
|
|
|
public LiveData<Boolean> getFragmentReady() {
|
|
return fragmentReady;
|
|
}
|
|
|
|
public void setFragmentReady(boolean ready) {
|
|
fragmentReady.setValue(ready);
|
|
}
|
|
|
|
// 清除数据,避免重复接收
|
|
|
|
// 清理所有数据的方法
|
|
public void clearAllData() {
|
|
dataForFragment.setValue(null);
|
|
childFragmentData.setValue(null);
|
|
seatViewTypeData.setValue(null);
|
|
fragmentReady.setValue(false);
|
|
}
|
|
|
|
// 清理子Fragment数据
|
|
public void clearChildFragmentData() {
|
|
childFragmentData.setValue(null);
|
|
}
|
|
|
|
// 清理主Fragment数据
|
|
public void clearFragmentData() {
|
|
dataForFragment.setValue(null);
|
|
}
|
|
} |