我有一个RecyclerView
在我ViewPager
的一个Fragment
。该RecyclerView
节目的数据是从服务器获取的。它通常可以工作,但我无法使用ViewModel
and实现相同的功能LiveData
,因为livedata.observe
当livedata
从ViewModel
.
这是我的MonthlyOrderViewModel.java类的源代码
public class MonthlyOrderViewModel extends AndroidViewModel {
private Retrofit retrofit;
private RetrofitServices retrofitServices;
private MutableLiveData<MonthlyOrderHistory> monthlyOrderHistoryMutableLiveData = new MutableLiveData<>();
public MonthlyOrderViewModel(@NonNull Application application) {
super(application);
retrofit = RetrofitFactory.getRetrofit();
retrofitServices = retrofit.create(RetrofitServices.class);
}
public MutableLiveData<MonthlyOrderHistory> getMonthlyOrderHistoryMutableLiveData() {
return monthlyOrderHistoryMutableLiveData;
}
public void fetchMonthlyOrders() {
Call<MonthlyOrderHistory> call = retrofitServices.getAllMonthlyOrderHistory(IpharmaApplication.getInstance().getLoggedInUser().getId());
call.enqueue(new Callback<MonthlyOrderHistory>() {
@Override
public void onResponse(Call<MonthlyOrderHistory> call, Response<MonthlyOrderHistory> response) {
MonthlyOrderHistory monthlyOrderHistory = response.body();
if(monthlyOrderHistory.getStatus().equalsIgnoreCase("success")) {
List<UserOrder> userOrders = monthlyOrderHistory.getOrders().getUserOrder();
if(userOrders != null && userOrders.size() != 0) {
DefaultScheduler.INSTANCE.execute(() -> {
monthlyOrderHistoryMutableLiveData.postValue(monthlyOrderHistory);
return Unit.INSTANCE;
});
}
}
}
@Override
public void onFailure(Call<MonthlyOrderHistory> call, Throwable t) {
t.printStackTrace();
}
});
}
}
DefaultScheduler
是一个Scheduler
默认处理AsyncScheduler
. 这是我的Scheduler.kt类的源代码
interface Scheduler {
fun execute(task: () -> Unit)
fun postToMainThread(task: () -> Unit)
fun postDelayedToMainThread(delay: Long, task: () -> Unit)
}
//A shim [Scheduler] that by default handles operations in the [AsyncScheduler].
object DefaultScheduler : Scheduler {
private var delegate: Scheduler = AsyncScheduler
//Sets the new delegate scheduler, null to revert to the default async one.
fun setDelegate(newDelegate: Scheduler?) {
delegate = newDelegate ?: AsyncScheduler
}
override fun execute(task: () -> Unit) {
delegate.execute(task)
}
override fun postToMainThread(task: () -> Unit) {
delegate.postToMainThread(task)
}
override fun postDelayedToMainThread(delay: Long, task: () -> Unit) {
delegate.postDelayedToMainThread(delay, task)
}
}
//Runs tasks in a [ExecutorService] with a fixed thread of pools
internal object AsyncScheduler : Scheduler {
private val executorService: ExecutorService = Executors.newFixedThreadPool(NUMBER_OF_THREADS)
override fun execute(task: () -> Unit) {
executorService.execute(task)
}
override fun postToMainThread(task: () -> Unit) {
if (isMainThread()) {
task()
} else {
val mainThreadHandler = Handler(Looper.getMainLooper())
mainThreadHandler.post(task)
}
}
private fun isMainThread(): Boolean {
return Looper.getMainLooper().thread === Thread.currentThread()
}
override fun postDelayedToMainThread(delay: Long, task: () -> Unit) {
val mainThreadHandler = Handler(Looper.getMainLooper())
mainThreadHandler.postDelayed(task, delay)
}
}
//Runs tasks synchronously.
object SyncScheduler : Scheduler {
private val postDelayedTasks = mutableListOf<() -> Unit>()
override fun execute(task: () -> Unit) {
task()
}
override fun postToMainThread(task: () -> Unit) {
task()
}
override fun postDelayedToMainThread(delay: Long, task: () -> Unit) {
postDelayedTasks.add(task)
}
fun runAllScheduledPostDelayedTasks() {
val tasks = postDelayedTasks.toList()
clearScheduledPostdelayedTasks()
for (task in tasks) {
task()
}
}
fun clearScheduledPostdelayedTasks() {
postDelayedTasks.clear()
}
}
最后,我正在尝试使用以下方法观察我的片段onCreateView
方法的变化:
MonthlyOrderViewModel monthlyOrderViewModel = new MonthlyOrderViewModel(getActivity().getApplication());
monthlyOrderViewModel.getMonthlyOrderHistoryMutableLiveData().observe(this, monthlyOrderHistory -> {
monthlyOrderRecyclerView = view.findViewById(R.id.monthlyOrderRecyclerView);
monthlyOrderRecyclerViewAdapter = new OrderHistoryRecyclerViewAdapter(getContext(), monthlyOrderHistory.getOrders().getUserOrder());
monthlyOrderRecyclerViewAdapter.notifyDataSetChanged();
monthlyOrderRecyclerView.setAdapter(monthlyOrderRecyclerViewAdapter);
monthlyOrderRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
});
我尝试调试它,发现改造后获取数据没有问题。但是observe
当我尝试在我的片段中使用它们时,该方法永远不会被调用。
请建议我如何解决这个问题?任何帮助,将不胜感激。