java – SwypeRefreshLayout doesn't stop animation in Fragment

Question:

There is a ViewPager, there are fragments in it. In a Fragment SwipeRefreshLayout with ListView Inside the Fragment class in onCreateView I set

swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.SwipeRefreshLayout);
    swipeLayout.setOnRefreshListener(SwipeRefreshListener);

    private SwipeRefreshLayout.OnRefreshListener SwipeRefreshListener = new SwipeRefreshLayout.OnRefreshListener(){
        @Override
        public void onRefresh() {
            swipeLayout.setRefreshing(true);
            new GetDataTask().execute();
        }
    };

Everything is working. After completing all the tasks in GetDataTask, I write swipeLayout.setRefreshing (false);, but the animation does not disappear. And so it hangs. Tried looking for something similar on stackoverflow but couldn't find it.

Maybe someone came across how to solve the problem? When there were no fragments before, everything worked.

They already suggested doing this on another resource:

@Override
public void onRefresh() {
    handler.postDelayed(stopRefreshing, 5000); // время можно уменьшить
}

private final Runnable stopRefreshing = new Runnable() {
    @Override
    public void run() {
        if (swipeLayout.isRefreshing()) swipeLayout.setRefreshing(false);
    }
};

But that doesn't help in any way. The strange thing is that it works every other time. And the problem came after I switched to fragments.

The class code of the task being executed:

private class GetDataTask extends AsyncTask<Void, Void, String[]> {
    @Override
    protected void onPostExecute(String[] result) {
        setDataAdapter("");
        swipeLayout.setRefreshing(false);
        super.onPostExecute(result);
    }
    @Override
    protected String[] doInBackground(Void... params) {
        updateListView_PullRefresh();
        return null;
    }
}

Answer:

Try this: 1) implement your snippet

 implements SwipeRefreshLayout.OnRefreshListener 

2) Declare your SwyperRefreshLayout like this:

refreshListViewLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refreshListViewLayout);
        refreshListViewLayout.setOnRefreshListener(this);

3) Close the update method

    @Override
        public void onRefresh() {
 refreshListViewLayout.setRefreshing(true);
            new GetDataTask().execute();
        }

4) In your GetDataTask inside onPostExecute execute refreshListViewLayout.setRefreshing(false);

This is how everything works for me.

Scroll to Top