简体   繁体   中英

Lifecycleowner in a ViewModel - Android Studio

I'm trying to implement ViewModel in a app which get some data from Firestore using the FirestorepagingAdapter, and displays it in a recyclerview. I'm already getting all data and displayig it, but still not using ViewModel, it's all on the MainActivity

I'm trying to move the code that creates Firestorepagingoptions to the the view model, and in my MainActivity, just create the adapter and set to the recyclerview. But the FirestorePagingOptions.Builder needs to set a LifecycleOwner and I don't know how to get it in my ViewModel, or maybe i should't be doing it on the ViewModel at all, I'm pretty lost yet with these ViewModel, so if anyone has any suggestion I appreciate. Thanks a lot.

Here's my original code on the MainActivity (not changed yet to get the data from the 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);
    }

And the code on my 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();
    }
}

Instead of setting the lifecycle owner in the options start and stop listening manually.

Start/stop listening The FirestorePagingAdapter listens for scrolling events and loads additional pages from the database only when needed.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM