Swipe Refresh in Textview

Asked

Viewed 339 times

0

I wonder if there is any way to implement a refresh Swipe in android studio to update only a textview when triggering? I’m using android studio, Activity has only a two text view at the top static that doesn’t change, six Buttons, and finally a textview that changes over time.

  • 1

    What you’ve already done?

  • As I did not know how this widget works. I prepared the whole XML.

2 answers

2

In your class, use the method setOnRefreshListener by changing the content of your TextView. That’s all:

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 may be something along those lines:

.
.
.
<!-- 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 more details here at how to use Swiperefreshlayouten in its application.

  • What does XML look like in this case? I built my XML without the Swiperefresh widget. All layout objects (Buttons and Static Texts) should be placed inside it, or just the text view.

  • @Joao as you did not put your XML, will be something close to this after editing.

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 your text TextView and also stop updating Swipe if you leave the method setRefreshing as true or not to inform that it should be false, the Swipe layout continue with the loading animation.

  • Getting here guys. VLW

Browser other questions tagged

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