ViewModel中的生命周期所有者-Android Studio

时间:2020-04-08 20:36:20

标签: android-studio google-cloud-firestore viewmodel lifecycleowner

我正在尝试在应用程序中实现ViewModel,该应用程序使用FirestorepagingAdapter从Firestore获取一些数据,并将其显示在recyclerview中。我已经获取了所有数据并进行了显示,但仍未使用ViewModel,它们全部在MainActivity上

我试图将创建Firestorepagingoptions的代码移至视图模型,在我的MainActivity中,只需创建适配器并将其设置为recyclerview。但是FirestorePagingOptions.Builder需要设置一个LifecycleOwner,我不知道如何在ViewModel中获取它,或者也许我根本不应该在ViewModel上使用它,我对这些ViewModel还是很迷惑,所以如果有人有任何建议,我感谢。非常感谢。

这是我在MainActivity上的原始代码(尚未更改以从Viewmodel获取数据)

public class ListaEmpresasActivity extends AppCompatActivity {

    private FirebaseFirestore firebaseDB;
    private RecyclerView recyclerViewListaEmpresas;
    private FirestorePagingAdapter adapter;
    private boolean viewChanged;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lista_empresas);

        recyclerViewListaEmpresas = findViewById(R.id.activity_main_lista_empresas);
        firebaseDB = FirebaseFirestore.getInstance();
        FirestorePagingOptions<EmpresaLista> options = configuraPaginacaoInicial();

        adapter = new ListaEmpresasAdapter(options);
        configuraRecyclerView(adapter);
    }

    private FirestorePagingOptions<EmpresaLista> configuraPaginacaoInicial() {
        Query query = firebaseDB.collection("Lista_Empresas").orderBy("id");
        return configuraOpcoesPaginacao(query);
    }

    private FirestorePagingOptions<EmpresaLista> configuraOpcoesPaginacao(Query query) {
        PagedList.Config config = new PagedList.Config.Builder()
                .setEnablePlaceholders(false)
                .setPrefetchDistance(10)
                .setPageSize(20)
                .build();

        return new FirestorePagingOptions.Builder<EmpresaLista>()
                .setLifecycleOwner(this)
                .setQuery(query, config, EmpresaLista.class)
                .build();
    }

    private void configuraRecyclerView(FirestorePagingAdapter adapter) {
        recyclerViewListaEmpresas.setHasFixedSize(true);
        recyclerViewListaEmpresas.setLayoutManager(new LinearLayoutManager(this));
        recyclerViewListaEmpresas.setAdapter(this.adapter);
    }

还有ViewModel上的代码

public class ListaEmpresasViewModel extends ViewModel {
    private MutableLiveData<FirestorePagingOptions<EmpresaLista>> options;
    private FirebaseFirestore firebaseDB;

    public LiveData<FirestorePagingOptions<EmpresaLista>> getOptions() {
        firebaseDB = FirebaseFirestore.getInstance();
        options = configuraPaginacaoInicial();
        return options;
    }

    private MutableLiveData<FirestorePagingOptions<EmpresaLista>> configuraPaginacaoInicial() {
        Query query = firebaseDB.collection("Lista_Empresas").orderBy("id");
        return configuraOpcoesPaginacao(query);
    }

    private MutableLiveData<FirestorePagingOptions<EmpresaLista>> configuraOpcoesPaginacao(Query query) {
        PagedList.Config config = new PagedList.Config.Builder()
                .setEnablePlaceholders(false)
                .setPrefetchDistance(10)
                .setPageSize(20)
                .build();

        return new FirestorePagingOptions.Builder<EmpresaLista>()
                .setLifecycleOwner()
                .setQuery(query, config, EmpresaLista.class)
                .build();
    }
}

1 个答案:

答案 0 :(得分:0)

不是在选项中设置生命周期所有者手动开始和停止监听。

开始/停止收听 FirestorePagingAdapter 侦听滚动事件并仅在需要时从数据库加载其他页面。

To begin populating data, call the startListening() method. You may want to call this in your onStart() method. Make sure you have finished any authentication necessary to read the data before calling startListening() or your query will fail.

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}
Similarly, the stopListening() call freezes the data in the RecyclerView and prevents any future loading of data pages.

Call this method when the containing Activity or Fragment stops:

@Override 
protected void onStop() {
    super.onStop();
    adapter.stopListening();
}