Update activity after swipe of the finger on the screen

3

How do I update APP activity after the user swipes the screen down. How do I detect this action so I can trigger an event in my code?

    
asked by anonymous 26.04.2015 / 01:41

1 answer

4

To use SwipeRefreshLayout is quite simple.

0 - Configuration

If you do not already use the 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 Gradle then you need to import the Support Library v4 by following this tutorial: support-library / setup .

1 - Add SwipeRefreshLayout as the 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 your Activity or Fragment , set 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 - Execute the action when SwipeRefreshLayout notify:

Your Activity should implement the SwipeRefreshLayout.OnRefreshListener interface.

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

4 - End the animation when data is loaded:

mSwipeToRefresh.setRefreshing(false);
    
26.04.2015 / 02:18