T0plomj3r
T0plomj3r

Reputation: 695

Android using SwipeRefreshLayout animation

Is it possible to use only SwipeRefreshLayout loading animation without really refreshing view i.e on initial loading lot's of data?

This animation:

enter image description here

I want to show swipe animation before any view is show, as preloader for view.

Tried different combinations on wiew create, when I use

swipeRefreshLayout.setRefreshing(true);

and

swipeRefreshLayout.setRefreshing(false);

Thing works fine, but my swipe listener then doesn't work, it doesn't even catches event.

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                SomeCustomFragment.refreshFragment();
                swipeRefreshLayout.setRefreshing(false);
            }
        });

Is that I'm trying to do even possible?

Upvotes: 0

Views: 1203

Answers (3)

Samuel Robert
Samuel Robert

Reputation: 11032

Views will be rendered on the screen after the onCreate() method. So you need to have a callback function that executes after the onCreate() method. Something like below will do the trick.

swipeRefreshLayout.post(new Runnable() {
        @Override
        public void run() {
                swipeRefreshLayout.setRefreshing(true);
                //Do your stuff here
        }
});

Remember to set the animation off by setting

swipeRefreshLayout.setRefreshing(false)

As soon as you're done with the task

Upvotes: 0

Priyank Patel
Priyank Patel

Reputation: 12382

Try with below code to trigger the swipe refresh layout programmatically.

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

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

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

Upvotes: 0

RoShan Shan
RoShan Shan

Reputation: 2954

You can try this:

refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(true);
                // load data
                .....
            }
        });

Upvotes: 0

Related Questions