Niklas
Niklas

Reputation: 25391

SwipeRefreshLayout trigger programmatically

Is there any way to trigger the SwipeRefreshLayout programmatically? The animation should start and the onRefresh method from the OnRefreshListener interface should get called.

Upvotes: 73

Views: 39941

Answers (7)

Ramz
Ramz

Reputation: 7164

if you are using the new swipeRefreshLayout intoduced in 5.0 enter image description here

As the image shown above you just need to add the following line to trigger the swipe refresh layout programmatically

Work

in Java:

 mSwipeRefreshLayout.post(new Runnable() {
     @Override
     public void run() {
         mSwipeRefreshLayout.setRefreshing(true);
     }
 });

on in Kotlin:

mSwipeRefreshLayout.post { mSwipeRefreshLayout.isRefreshing = true }

NOT work

if you simply call in Java:

 mSwipeRefreshLayout.setRefreshing(true);

or in Kotlin

 mSwipeRefreshLayout.isRefreshing = true

it won't trigger the circle to animate, so by adding the above line u just make a delay in the UI thread so that it shows the circle animation inside the ui thread.

By calling mSwipeRefreshLayout.setRefreshing(true) the OnRefreshListener will NOT get executed

In order to stop the circular loading animation call mSwipeRefreshLayout.setRefreshing(false)

Upvotes: 150

Aman Raj Sundram
Aman Raj Sundram

Reputation: 139

Simply create a SwipeRefreshLayout.OnRefreshListener and call its function onRefresh() whenever needed:

SwipeRefreshLayout srl;
SwipeRefreshLayout.OnRefreshListener refreshListener;

srl = (SwipeRefreshLayout)v.findViewById(R.id.swipeRefreshLayout);

refreshListener = new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
      //Do your stuff here
    }
};
srl.setOnRefreshListener(refreshListener);

Now, whenever you want to call it manually, just call it through the listener

refreshListener.onRefresh();

Upvotes: 3

Indiana
Indiana

Reputation: 723

Bit late to the thread, but you do not need to launch a Runnable to do this. You can simply trigger the refresh and call your onRefresh method directly in onCreate, or wherever you want this to happen:

class MyFragment: SwipeRefreshLayout.OnRefreshListener {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
    }

    fun initViews() {
        swipe_refresh_layout?.apply {
            setOnRefreshListener(this@MyFragment)
            isRefreshing = true
            onRefresh()
        }
    }

    override fun onRefresh() {
        // Do my refresh logic here
    }
}

Upvotes: 1

Aditya Pratama
Aditya Pratama

Reputation: 11

binding.swipeRefreshLayout.setRefreshing(true); // show loading 
binding.swipeRefreshLayout.post(this::updateUI); // call method
binding.swipeRefreshLayout.setOnRefreshListener(this::updateUI); // call method

Upvotes: 0

Goodlife
Goodlife

Reputation: 3922

Just to force in add this two to ennable swipe gesture

swipeRefreshLayout.setOnRefreshListener(
    new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            Log.i(TAG, "onRefresh called from SwipeRefreshLayout");

            // This method performs the actual data-refresh operation.
            // The method calls setRefreshing(false) when it's finished.
            FetchData();
        }
    }
);

Upvotes: -2

user7010102
user7010102

Reputation:

You can call onRefresh() method programmatically and then inside the method start the animation if it is not already started. See the following:

@Override
public void onRefresh() {
    if (!mSwipeRefreshLayout.isRefreshing()) mSwipeRefreshLayout.setRefreshing(true);
    //TODO
}

Upvotes: -1

Min2
Min2

Reputation: 10971

In order to trigger SwipeRefreshLayout I tried this solution:

SwipeRefreshLayout.OnRefreshListener swipeRefreshListner = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            Log.i(TAG, "onRefresh called from SwipeRefreshLayout");
            // This method performs the actual data-refresh operation.
            // The method calls setRefreshing(false) when it's finished.
            loadData();
        }
    };

Now key part:

swipeLayout.post(new Runnable() {
@Override public void run() {
     swipeLayout.setRefreshing(true);
     // directly call onRefresh() method 
     swipeRefreshListner.onRefresh();
   }
});

Upvotes: 29

Related Questions