我有以下viewmodel:
public class Feedviewmodel extends viewmodel { private final mutablelivedata<Photo> selectedPhoto = new mutablelivedata<>(); private final FeedRepository repository; private LiveData<Resource<List<Photo>>> Feed; @Inject Feedviewmodel(@NonNull final FeedRepository repository) { this.repository = repository; } public void selectPhoto(@NonNull final Photo photo) { selectedPhoto.setValue(photo); } public LiveData<Photo> getSelectedPhoto() { return selectedPhoto; } LiveData<Resource<List<Photo>>> getUserFeed() { if (Feed == null) { Feed = new mutablelivedata<>(); Feed = repository.get(); } return Feed; } @Override protected void onCleared() { super.onCleared(); }
}
我用这种方式观察Fragment中的Feed:
@Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); viewmodel.getUserFeed().observe(this,Feed -> { switch (Feed.status) { case ERROR: processErrorState(Feed.data); break; case LOADING: processLoadingState(Feed.data); break; case SUCCESS: if (Feed.data != null) processSuccessstate(Feed.data); break; } }); }
问题是:我怎样才能以正确的方式刷新饲料?假设用户触发了swipetoRefresh,因此该事件必须创建一个Feed的刷新任务.我如何使用viewmodel实现这一点?