【发布时间】:2018-06-04 19:12:43
【问题描述】:
我正在尝试在我的fragment 之一中实现Circular ProgressBar。
我想使用AsyncTask 类和其他后台进程来实现这一点,我希望progress bar 也能更新onProgressUpdate() 方法。
这是我实现 ProgressBar 的 Fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.playlist_fragment, container, false);
// Setting up progressBar
progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
progressBar.setProgress(0);
return rootView;
}
从 Fragment 中调用 AsyncTask 类,该类表示无法解析 setProgressBr() 的方法。我认为我没有适当地传递参数。
new GetPlaylistAsyncTask(mYouTubeDataApi,getContext(),PlaylistFragment.this)
.setProgressBar(progressBar)
.execute(playlistVideos.playlistId, playlistVideos.getNextPageToken());
这是我的 AsyncTask 类:
public class GetPlaylistAsyncTask extends AsyncTask<String, Integer, Pair<String, List<Video>>> {
private static final String TAG = "GetPlaylistAsyncTask";
private static final Long YOUTUBE_PLAYLIST_MAX_RESULTS = 10L;
//see: https://developers.google.com/youtube/v3/docs/playlistItems/list
private static final String YOUTUBE_PLAYLIST_PART = "snippet";
private static final String YOUTUBE_PLAYLIST_FIELDS = "pageInfo,nextPageToken,items(id,snippet(resourceId/videoId))";
//see: https://developers.google.com/youtube/v3/docs/videos/list
private static final String YOUTUBE_VIDEOS_PART = "snippet,contentDetails,statistics"; // video resource properties that the response will include.
private static final String YOUTUBE_VIDEOS_FIELDS = "items(id,snippet(title,description,thumbnails/high),contentDetails/duration,statistics)"; // selector specifying which fields to include in a partial response.
private YouTube mYouTubeDataApi;
Context mContext;
ProgressBar bar;
private AsyncResponse theListener;
public void setProgressBar(ProgressBar bar) {
this.bar = bar;
}
public GetPlaylistAsyncTask(YouTube api, Context context, PlaylistFragment frag ) {
mYouTubeDataApi = api;
mContext = context;
theListener = (AsyncResponse)frag;
}
@Override
protected Pair<String, List<Video>> doInBackground(String... params) {
final String playlistId = params[0];
final String nextPageToken;
if (params.length == 2) {
nextPageToken = params[1];
} else {
nextPageToken = null;
}
PlaylistItemListResponse playlistItemListResponse;
try {
playlistItemListResponse = mYouTubeDataApi.playlistItems()
.list(YOUTUBE_PLAYLIST_PART)
.setPlaylistId(playlistId)
.setPageToken(nextPageToken)
.setFields(YOUTUBE_PLAYLIST_FIELDS)
.setMaxResults(YOUTUBE_PLAYLIST_MAX_RESULTS)
.setKey(ApiKey.YOUTUBE_API_KEY)
.execute();
} catch (IOException e) {
e.printStackTrace();
return null;
}
if (playlistItemListResponse == null) {
Log.e(TAG, "Failed to get playlist");
return null;
}
List<String> videoIds = new ArrayList();
// pull out the video id's from the playlist page
for (PlaylistItem item : playlistItemListResponse.getItems()) {
videoIds.add(item.getSnippet().getResourceId().getVideoId());
}
// get details of the videos on this playlist page
VideoListResponse videoListResponse = null;
try {
videoListResponse = mYouTubeDataApi.videos()
.list(YOUTUBE_VIDEOS_PART)
.setFields(YOUTUBE_VIDEOS_FIELDS)
.setKey(ApiKey.YOUTUBE_API_KEY)
.setId(TextUtils.join(",", videoIds)).execute();
} catch (IOException e) {
e.printStackTrace();
}
return new Pair(playlistItemListResponse.getNextPageToken(), videoListResponse.getItems());
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Integer... values) {
if (this.bar != null) {
bar.setProgress(values[0]);
}
}
@Override
protected void onPostExecute(Pair<String, List<Video>> result) {
theListener.handleGetPlaylistResult(null,result);
}
}
希望能快速解决上述问题。提前致谢。
【问题讨论】:
标签: android android-fragments android-asynctask android-progressbar