我创建了一个通用的AsyncTask。它在后台工作正常但是当我在doInBackground回调此任务时更新UI时,它只是多次挂起SwipeRefreshLayout的加载圈。我需要这个加载才能顺利运行。 我正在使用AsyncTask如下
public class backgroundWorker extends AsyncTask<Object, Void, Object>{//params, progress, result
backgroundWorkerCallback callback;
public backgroundWorker(backgroundWorkerCallback _callback) {
this.callback = _callback;
}
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
callback.onPostExecute(result);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Object doInBackground(Object... param)
{
return callback.doInBackground(param);
}
}
backgroundWorkerCallback是一个界面
public interface backgroundWorkerCallback {
public Object doInBackground(Object param);
public void onPostExecute(Object result);
}
我正在以这种方式使用这个AsyncTask
srlStockReport.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
backgroundWorker bw = new backgroundWorker(new backgroundWorkerCallback(){
@Override
public Object doInBackground(Object param)
{
//fetch data from server
//put some data in in Preference
//Load a RecyclerView from list
return null;
}
@Override
public void onPostExecute(Object result)
{
}
});
bw.execute();
}
});