SwipeRefreshLayout animation frozen

72 views Asked by At

I am trying to use swipe refresh layout to update some data. All works fine but a small bug that I can't seem to solve

When I pull to refresh, the animation gets frozen.

I have implemented it this way:

myRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        // Do some stuff
        // Sleep as a demonstrator of the issue
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        myRefreshLayout.setRefreshing(false);
    }
});

and also tried this:

myRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        myRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                // Do some stuff
                // Sleep as a demonstrator of the issue
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        myRefreshLayout.setRefreshing(false);
                    }
                });
            }
        });
    }
});

but the animation remains frozen until the everything is done, and then it just vanishes (because it hits the setRefreshing(false))

After searching a little, I thought it could be the UI that is waiting for the stuff to finish, so I tried to implement it this way:

myRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // Do some stuff
                    // Sleep as a demonstrator of the issue
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            myRefreshLayout.setRefreshing(false);
                        }
                    });
                }
            });
        }
    });
}

What happens in this case is that the refresh indicator never vanishes, so I guess it is not calling the runOnUiThread I tried the same thing with the main looper (handler) instead of the runOnUiThread with the same effect.

Is there any neat way of implementing this? Am I missing some detail?

I tried the steps described above and I also looked on Stack Overflow for similar issues to no avail.

Thanks!

1

There are 1 answers

3
lawrence On

It's right to do refreshing on background thread,and setRefreshing(false) on main thread. The problem is you forgot to call start() after new thread.

myRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // Do some stuff
                    // Sleep as a demonstrator of the issue
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            myRefreshLayout.setRefreshing(false);
                        }
                    });
                }
            }).start();// start the thread here
        }
    });
}