问题描述
我的应用程序是一个基本的新闻应用程序,可从Guardian API提供的JSON中获取数据.我使用原始Java代码(不使用翻新版)从JSON解析了值.
My app is a basic news app which fetches data from JSON provided by Guardian API.I parsed the values from JSON using raw java code (not using retrofit).
然后我在NewsFeedViewModel类中获得了LiveData,该类扩展为AndroidViewModel.
Then I get the LiveData in NewsFeedViewModel class which extends as AndroidViewModel.
然后在片段中,我将列表提交给适配器.
And then in the fragment, I submit list to adapter.
这些是我面临的问题:1)首先,如果要显示的文章设置为10,则如果我转到设置并将其更改为2,则最后8篇文章消失了,但是空白/gap却没有.我仍然可以在空白处滚动.2)如果我不断更改商品数量值,那么应用程序将变得不可滚动.
These are the issues I'm facing:1) at first, if the articles to show is set to 10, then if i go to settings and change it to 2, then the last 8 articles are disappearing but the white space /gap is not going. I can still scroll through the empty gap.2) if i change the number of articles value constantly, then app is becoming un-scrollable.
还有其他一些疑问,当发生swipeToRefresh时如何手动刷新数据?
And i have a few more doubts, how to refresh the data manually when swipeToRefresh is happened?
这是我的项目github链接: https://github.com/sdzshn3/News24-7-RV
This is my project github link: https://github.com/sdzshn3/News24-7-RV
应用中发生的问题的视频示例: https://drive.google.com/file/d/1gr_fabS2rqREuyecvGSG3IQ_jXOowlW7/view?usp=drivesdk
Video sample of the issue happening in app: https://drive.google.com/file/d/1gr_fabS2rqREuyecvGSG3IQ_jXOowlW7/view?usp=drivesdk
推荐答案
您需要完全按照我在此Reddit帖子:
public class RefreshLiveData<T> extends MutableLiveData<T> {
public interface RefreshAction<T> {
private interface Callback<T> {
void onDataLoaded(T t);
}
void loadData(Callback<T> callback);
}
private final RefreshAction<T> refreshAction;
private final Callback<T> callback = new RefreshAction.Callback<T>() {
@Override
public void onDataLoaded(T t) {
postValue(t);
}
};
public RefreshLiveData(RefreshAction<T> refreshAction) {
this.refreshAction = refreshAction;
}
public final void refresh() {
refreshAction.loadData(callback);
}
}
那你就可以做
public class YourViewModel extends ViewModel {
private final GithubRepository githubRepository;
public YourViewModel(GithubRepository githubRepository, SavedStateHandle savedStateHandle) {
this.githubRepository = githubRepository;
}
private final LiveData<String> userId = savedStateHandle.getLiveData("userId"); // from args
private final RefreshLiveData<List<Project>> refreshLiveData = Transformations.switchMap(userId, (uId) -> {
return githubRepository.getProjectList(uId);
});
public void refreshData() {
refreshLiveData.refresh();
}
public LiveData<List<Project>> getProjects() {
return refreshLiveData;
}
}
然后存储库可以执行以下操作:
And then repository can do:
public RefreshLiveData<List<Project>> getProjectList(String userId) {
final RefreshLiveData<List<Project>> liveData = new RefreshLiveData<>((callback) -> {
githubService.getProjectList(userId).enqueue(new Callback<List<Project>>() {
@Override
public void onResponse(Call<List<Project>> call, Response<List<Project>> response) {
callback.onDataLoaded(response.body());
}
@Override
public void onFailure(Call<List<Project>> call, Throwable t) {
}
});
});
return liveData;
}
这篇关于如何在Android中手动重新正确加载liveData?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!