如何在Android中手动重新正确加载liveData? [英] How to properly reload liveData manually in Android?

查看:131
本文介绍了如何在Android中手动重新正确加载liveData?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是一个基本的新闻应用程序,可从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

推荐答案

您需要完全按照我在那你就可以做

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆