Swipe Refresh in TextView

0

I wonder if there is any way to implement a swipe refresh in android studio to update only one textview when triggering? I'm using android studio, the activity has only a two text view on the top static that does not change, six buttons, and finally a textview that changes over time.

    
asked by anonymous 04.08.2017 / 20:14

2 answers

0
swipeLayout.setOnRefreshListener(
    new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            updateMessage("Hi, Jarbas");
        }
    }
);

// ...
private void updateMessage(String msg) {
    textMessage.setText(msg);
    swipeLayout.setRefreshing(false);
}

This will update the text of your TextView and also stop updating the Swipe, if you leave the method setRefreshing to true or not inform that it should be false, the swipe layout will continue with the loading animation.

    
04.08.2017 / 20:26
2

In your class, use the setOnRefreshListener method by changing the contents of your TextView . Just this:

SwipeRefreshLayout swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
       meuTextView.setText("Aqui o novo texto ao usar o swipe refresh");
       swipeLayout.setRefreshing(false);
    }
});

XML

Your .xml might be something like this:

.
.
.
<!-- aqui suas outras views se houver-->

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <!-- aqui suas outras views se houver-->

    <TextView
        android:id="@+id/meuTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Jon Snow"
        android:textSize="40dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />


    <!-- aqui suas outras views se houver-->

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

See how to use SwipeRefreshLayout in in your application.

    
04.08.2017 / 20:22