在 ViewPager 管理的片段中使用 AsyncTaskLoaders 时遇到问题。配置是这样的:
Fragment#1 - 从磁盘加载图片的加载器 Fragment#2 - 从磁盘加载自定义对象的加载器 Fragment#3 - 从网络加载 JSON 对象的加载器 Fragment#4 - 作为 #3
如果我在 Fragment#1 中省略了 Loader,一切正常,如预期的那样。但是,如果我尝试在 Fragment#1 中使用 Loader,所有其他 Loader 都不会调用每个片段中指定的 onLoadFinished 回调。我放了一些日志,我看到所有其他加载程序都正确处理了 loadInBackground() 方法,但在那之后它们处于重置状态。
也许有最大数量的同时可用的加载器,或者加载的位图大小有问题?
每个片段都需要在onCreateView()回调中初始化加载程序,因为
getLoaderManager().initLoader(STATS_LOADER_ID, null, this);
并以此实现LoaderManager.LoaderCallbacks
@Override
public Loader<UserStats> onCreateLoader(int arg0, Bundle arg1) {
ProfileStatsLoader loader = new ProfileStatsLoader(getActivity(), userId);
loader.forceLoad();
return loader;
}
@Override
public void onLoadFinished(Loader<UserStats> loader, UserStats stats) {
Log.d(TAG, "onLoadFinished");
postLoadUI((ProfileStatsLoader) loader, stats);
}
@Override
public void onLoaderReset(Loader<UserStats> arg0) {
Log.d(TAG, "onLoaderReset");
}
第一个 Loader(这可能是其余三个失败的原因)代码是
public class ProfilePictureLoader extends AsyncTaskLoader<Bitmap> {
private static final String TAG = ProfilePictureLoader.class.getCanonicalName();
private User user;
private int maxSize;
private Bitmap bitmap;
private Throwable error;
public ProfilePictureLoader(Context context, User user, int maxSize) {
super(context);
this.user = user;
this.maxSize = maxSize;
}
public Throwable getError() {
return error;
}
public Bitmap getPicture() {
return bitmap;
}
@Override
public Bitmap loadInBackground() {
Log.d(TAG, "Loading bitmap for user " + user.getUuid());
Log.w(TAG, "Is reset at start? " + this.isReset());
ImageUtils iu = new ImageUtils(getContext());
String localProfilePicturePath = iu.loadBitmapURIForUser(user.getUuid());
// if the user has not a picture saved locally (for logged user) neither
// have never set a profile picture (for online users), return a null
// bitmap
if (localProfilePicturePath == null && !user.hasProfilePicture())
return null;
try {
if (user.isStoredLocally()) {
// load local profile image
bitmap = null;
if (localProfilePicturePath != null)
// load profilePictureMaxSize px width/height
bitmap = ImageUtils.decodeSampledBitmapFromPath(localProfilePicturePath, maxSize, maxSize);
} else {
// download remotely
bitmap = UserRESTClient.downloadUserPicture(user.getUuid(), UserRESTClient.PICTURE_MEDIUM_SIZE);
}
return bitmap;
} catch (Throwable t) {
Log.e(TAG, "Error loading bitmap for user " + user.getUuid() + ": " + t);
t.printStackTrace();
error = t;
return null;
}finally{
Log.w(TAG, "Is reset? " + this.isReset());
}
}
}