简体   繁体   中英

Keep SwipeRefreshLayout until task is finished running

Hi so I have the following code:

In the onCreate:

mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                refreshContent();
            }
        });

And in another method:

private void refreshContent(){
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            db = new DatabaseHelper(getApplicationContext());
            new RefreshScores().execute();
            list.clear();
            list.addAll(db.getTracked());
            db.closeDB();
            mSwipeRefreshLayout.setRefreshing(false);
            adapter.notifyDataSetChanged();
        }
    },2000);
}

Currently, when I pull down near the top of my app, the refresh spinner shows up and starts spinning. It disappears after 2 seconds. However, I want it to remain visible until RefreshScores().execute() and list.clear(); list.addAll(db.getTracked()); db.closeDB(); list.clear(); list.addAll(db.getTracked()); db.closeDB(); have occured. The RefreshScores is an AsyncTask. Not really sure how to do this.

AsyncTask has a callback named onPostExecute . This callback will be invoked in the UI thread when doInBackground() returns. See http://developer.android.com/reference/android/os/AsyncTask.html

You may try using

mSwipeRefreshLayout.setRefreshing(false);

directly in this callback, and move other calls which require new data to the callback as well.

Edit

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

                            }
                        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM