Refresh activity after swiping your finger on the screen

Asked

Viewed 2,332 times

2

How do I update the APP activity after the user swipes their finger down on the screen. How to detect this action to trigger an event in my code?

  • You could use the SwipeRefreshLayout which is present in support-library-v4.

  • Hello Wakim! I would appreciate it if you put up an example of use.

1 answer

5


To use the SwipeRefreshLayout is quite simple.

0 - Configuration

If you have not yet used Support Library v4, then add the same to your project:

dependencies {
    // Demais dependencias do seu projeto
    compile 'com.android.support:support-v4:22.1.1'
}

If you do not use the Gradle, then it is necessary to import the Support Library v4 following this tutorial: support-library/setup.

1 - Add the SwipeRefreshLayout as root of your layout.

An example:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipe_refresh_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Restante das views do seu layout -->

</android.support.v4.widget.SwipeRefreshLayout>

2 - In its Activity or Fragment, configure the SwipeRefreshLayout:

// Recupera o SwipeRefreshLayout
mSwipeToRefresh = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_container);

// Seta o Listener para atualizar o conteudo quando o gesto for feito
mSwipeToRefresh.setOnRefreshListener(this);

// O esquema de cores
mSwipeToRefresh.setColorSchemeResources(
    R.color.indigo_300,
    R.color.indigo_400,
    R.color.indigo_500,
    R.color.indigo_600,
    R.color.indigo_700,
    R.color.indigo_800,
    R.color.indigo_900
);

3 - Perform the action when the SwipeRefreshLayout notify:

Your Activity should implement the interface SwipeRefreshLayout.OnRefreshListener.

@Override 
public void onRefresh() {
    // Executar a atualizacao
}

4 - Finish animation when data is loaded:

mSwipeToRefresh.setRefreshing(false);
  • Hello Wakim! Thanks for the example, but I’m still not able to build an example. Could you send me a project ready for Eclipse?

  • @Vitormendanha, unfortunately I no longer use the Eclipse in a long time, I can try later to build a functional example. But it would be faster maybe you keep adding the question the mistakes you are having, to get to the solution.

  • I will test with Android Studio. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.