这是在Fragment中使用的:
@Override
public void onActivityCreated(@Nullable
Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
videoListView=(ListView)getActivity().findViewById(R.id.videoListView);
allVideoList = new
ArrayList<VideoModel>();
//实例化该任务,调用方法启动
CountingTask task=new CountingTask();
task.execute();
}
自定义AsyncTask类:
private class
CountingTask extends AsyncTask<Void, Integer, Integer>{
CountingTask(){}
@Override
/**
* 在后台运行并处理后台操作
*/
protected Integer doInBackground(Void... params) {
int i=0;
while(i<100){
SystemClock.sleep(250);
i++;
if(i%5==0){
//每5%进度时更新一次UI
publishProgress(i);
}
}
return i;
}
/**
* 将后台操作与主UI线程联系起来的方法,数据更新时调用
* @param progress 完成度
*/
protected void onProgressUpdate(Integer... progress){
$.openLoading();
}
/**
* 将后台操作与主UI线程联系起来的方法,完成时调用
* @param result 结果
*/
protected void onPostExecute(Integer result){
initData();
$.closeLoading();
}
}