我想从网上下载一堆图像,然后使用我的自定义 PagerAdapter 显示它们。所以我可以滑动浏览所有这些图像。
我定义了一个名为 as 的活动,PhotoSlideActivity
它从FragmentActivity
. 在这个活动中,我有自己的PagerAdapter
. 以下是我的代码:
private class PhotoSlidePagerAdapter extends FragmentStatePagerAdapter {
private final int mSize;
public PhotoSlidePagerAdapter(FragmentManager fm, int size) {
super(fm);
mSize = size;
}
@Override
public Fragment getItem(int position) {
PhotoUrl photoUrl = new PhotoUrl(position);
return PhotoSlidePageFragment.create(photoUrl.makePhotoUrl());
}
@Override
public int getCount() {
return mSize;
}
}
以下是我的PhotoSlidePageFragment
. mImageFetcher
是一个异步下载图像的类。
public class PhotoSlidePageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_photo_slide_page, container, false);
mImageView = (ImageView) v.findViewById(R.id.imageView1);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Use the parent activity to load the image asynchronously into the ImageView
if (PhotoSlideActivity.class.isInstance(getActivity())) {
mImageFetcher = ((PhotoSlideActivity) getActivity()).getImageFetcher();
mImageFetcher.loadImage(mImageUrl, mImageView);
}
}
(1) 我最初的想法是把mImageFetcher.loadImage(mImageUrl, mImageView);
. put it in和 inonCreateView
有什么区别吗?onCreateView
onActivityCreated
(2)如果我用 替换我所做的onActivityCreated
怎么mImageFetcher = new ImageFetcher(); mImageFetcher.loadImage(mImageUrl, mImageView);
办?这样做意味着使用Fragment
异步加载图像。这样做有什么不好的影响吗?