Android体系结构组件LiveData [英] Android Architecture Components LiveData

查看:95
本文介绍了Android体系结构组件LiveData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用体系结构组件来实现一个简单的应用程序. 我可以使用Retrofit2从RestApi服务获取信息. 我可以在相应的Recyclerview中显示该信息,当我旋转手机时,一切都会正常进行. 现在我想按一种新的对象(按字符串)进行过滤

I'm trying to implement a simple App using Architecture Components. I can get the info from RestApi services using Retrofit2. I can show the info in the respective Recyclerview and when I rotate the phone everything works as it should. Now I want to filter by a new kind of object (by string)

有人可以用ViewModel指导我一点,我不知道这样做的最佳实践是什么... 我正在使用MVVM ...

Can someone guide me a little with the ViewModel, I don't know what is the best practice to do that... I'm using MVVM...

这是我的ViewModel:

This is my ViewModel:

public class ListItemViewModel extends ViewModel {
    private MediatorLiveData<ItemList> mList;
    private MeliRepository meliRepository;

    /* Empty Contructor.
     * To have a ViewModel class with non-empty constructor,
     * I have to create a Factory class which would create instance of you ViewModel and
     * that Factory class has to implement ViewModelProvider.Factory interface.
     */
    public ListItemViewModel(){
        meliRepository = new MeliRepository();
    }

    public LiveData<ItemList> getItemList(String query){
       if(mList == null){
           mList = new MediatorLiveData<>();
           LoadItems(query);
       }
    }

    private void LoadItems(String query){
        String queryToSearch = TextUtils.isEmpty(query) ? "IPOD" : query;

        mList.addSource(
                meliRepository.getItemsByQuery(queryToSearch),
                list -> mList.setValue(list)
        );
    }
}

更新

我通过使用生命周期库中的包进行转换来解决了这个问题... 在此处输入链接描述

I resolved this using transformation a package from lifecycle library... enter link description here

public class ListItemViewModel extends ViewModel {

    private final MutableLiveData<String> mQuery = new MutableLiveData<>();
    private MeliRepository meliRepository;
    private LiveData<ItemList> mList = Transformations.switchMap(mQuery, text -> {
        return meliRepository.getItemsByQuery(text);
    });

    public ListItemViewModel(MeliRepository repository){
        meliRepository = repository;
    }

    public LiveData<ItemList> getItemList(String query){
       return mList;
    }
}

@John,这是我的解决方案.我正在使用生命周期库,解决方案比我想象的要容易.谢谢!

@John this is my solution. I'm using lifecycle library and the solution was easier than I thought. Thx!

推荐答案

在Kotlin中我对此比较熟悉,但是您应该能够轻松地将其翻译成Java(或者现在是开始使用Java的好时机) Kotlin :))....采用类似的模式,我相信您会做类似的事情:

I'm more familiar with doing this in Kotlin but you should be able to translate this to Java easily enough (or perhaps now is a good time to start using Kotlin :) )....adapting similar pattern I have here I believe you'd do something like:

val query: MutableLiveData<String> = MutableLiveData()

val  mList = MediatorLiveData<List<ItemList>>().apply {
    this.addSource(query) {
        this.value = meliRepository.getItemsByQuery(query)
    }
}

fun setQuery(q: String) {
    query.value = q
}

我在以下 查看全文

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