Questo documento mostra come aggiornare l'app quando l'utente richiede un aggiornamento manuale, indipendentemente dal fatto che lo attivi con un gesto di scorrimento o utilizzi l'azione di aggiornamento della barra delle azioni.
Rispondere al gesto di aggiornamento
Quando l'utente esegue il gesto di scorrimento per aggiornare, il sistema visualizza l'indicatore di avanzamento e chiama il metodo di callback dell'app. Il metodo di callback è responsabile dell'aggiornamento dei dati dell'app.
Per rispondere al gesto di aggiornamento nella tua app, implementa l'interfaccia
SwipeRefreshLayout.OnRefreshListener
e il relativo metodo
onRefresh(). Il metodo onRefresh() viene richiamato quando l'utente esegue un
movimento di scorrimento.
Inserisci il codice per l'operazione di aggiornamento effettiva in un metodo separato, preferibilmente
in un ViewModel, e chiama il metodo di aggiornamento dall'implementazione
di onRefresh(). In questo modo, puoi utilizzare lo stesso metodo di aggiornamento per eseguire l'aggiornamento quando l'utente attiva un aggiornamento dalla barra delle azioni.
Nel metodo di aggiornamento, chiama
setRefreshing(false)
al termine dell'aggiornamento dei dati. La chiamata di questo metodo indica a
SwipeRefreshLayout
di rimuovere l'indicatore di avanzamento e aggiornare i contenuti della visualizzazione.
Ad esempio, il seguente codice implementa onRefresh() e
richiama il metodo myUpdateOperation() per aggiornare i dati visualizzati
da un
ListView:
Kotlin
// Sets up a SwipeRefreshLayout.OnRefreshListener that invokes when // the user performs a swipe-to-refresh gesture. mySwipeRefreshLayout.setOnRefreshListener { Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout") // This method performs the actual data-refresh operation and calls // setRefreshing(false) when it finishes. myUpdateOperation() }
Java
// Sets up a SwipeRefreshLayout.OnRefreshListener that is invoked when // the user performs a swipe-to-refresh gesture. mySwipeRefreshLayout.setOnRefreshListener(() -> { Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout"); // This method performs the actual data-refresh operation and calls // setRefreshing(false) when it finishes. myUpdateOperation(); } );
Rispondere all'azione di aggiornamento
Se l'utente richiede un aggiornamento utilizzando la barra delle azioni, il sistema chiama il metodo
onOptionsItemSelected(). La tua app risponde a questa chiamata visualizzando l'indicatore di avanzamento e
aggiornando i dati dell'app.
Per rispondere all'azione di aggiornamento, esegui l'override di
onOptionsItemSelected(). Nel metodo di override, attiva l'indicatore di avanzamento SwipeRefreshLayout chiamando setRefreshing() con il valore true, poi esegui l'operazione di aggiornamento. Esegui l'aggiornamento effettivo in un metodo separato, in modo che lo stesso metodo possa essere chiamato indipendentemente dal fatto che l'utente attivi l'aggiornamento con uno scorrimento o utilizzi la barra delle azioni. Al termine dell'aggiornamento, chiama setRefreshing(false)
per rimuovere l'indicatore di avanzamento dell'aggiornamento.
Il seguente codice mostra come rispondere all'azione di richiesta:
Kotlin
// Listen for option item selections to receive a notification when the user // requests a refresh by selecting the refresh action bar item. override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { // Check whether the user triggers a refresh: R.id.menu_refresh -> { Log.i(LOG_TAG, "Refresh menu item selected") // Signal SwipeRefreshLayout to start the progress indicator. mySwipeRefreshLayout.isRefreshing = true // Start the refresh background task. This method calls // setRefreshing(false) when it finishes. myUpdateOperation() return true } } // User doesn't trigger a refresh. Let the superclass handle this action. return super.onOptionsItemSelected(item) }
Java
// Listen for option item selections to receive a notification when the user // requests a refresh by selecting the refresh action bar item. @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Check whether the user triggers a refresh: case R.id.menu_refresh: Log.i(LOG_TAG, "Refresh menu item selected"); // Signal SwipeRefreshLayout to start the progress indicator. mySwipeRefreshLayout.setRefreshing(true); // Start the refresh background task. This method calls // setRefreshing(false) when it finishes. myUpdateOperation(); return true; } // User doesn't trigger a refresh. Let the superclass handle this action. return super.onOptionsItemSelected(item); }