Rispondere a una richiesta di aggiornamento

Prova la funzionalità Scrivi
Jetpack Compose è il toolkit per l'interfaccia utente consigliato per Android. Scopri come aggiornare i contenuti scorrendo verso il basso in Scrivi.

Questo documento mostra come aggiornare l'app quando l'utente richiede un aggiornamento manuale, che lo attivi con un gesto di scorrimento o utilizzi l'azione di aggiornamento della barra delle app.

Rispondere al gesto di aggiornamento

Quando l'utente esegue il gesto di scorrimento per aggiornare, il sistema mostra 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 invocato quando l'utente esegue un gesto di scorrimento.

Inserisci il codice per l'operazione di aggiornamento effettiva in un metodo separato, preferibilmente in un ViewModel, e chiama questo metodo di aggiornamento dall'implementazione onRefresh(). In questo modo, puoi utilizzare lo stesso metodo di aggiornamento per eseguire l'aggiornamento quando l'utente attiva un aggiornamento dalla barra di azione.

Nel metodo di aggiornamento, chiama setRefreshing(false) al termine dell'aggiornamento dei dati. La chiamata a 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 invoca 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 app, il sistema chiama il metodo onOptionsItemSelected(). L'app risponde a questa chiamata mostrando l'indicatore di avanzamento e aggiornando i dati dell'app.

Per rispondere all'azione di aggiornamento, sostituisci onOptionsItemSelected(). Nel metodo di override, attiva l'indicatore di avanzamento SwipeRefreshLayout chiamando setRefreshing() con il valore true, quindi 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 app. Al termine dell'aggiornamento, chiama setRefreshing(false) per rimuovere l'indicatore dell'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);
}