2013-05-20 57 views
0

我想從網上下載一堆圖像,然後使用我的自定義PagerAdapter顯示它們。所以我可以滑動瀏覽所有這些圖像。android使用ViewPager來顯示圖像

我定義了一個名爲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; 
     } 
    } 

以下是我的PhotoSlidePageFragmentmImageFetcher是一個異步下載圖像的類。

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);onCreateView。把它放在onCreateViewonActivityCreated之間有什麼區別嗎?

(2)如果我將onActivityCreated替換成mImageFetcher = new ImageFetcher(); mImageFetcher.loadImage(mImageUrl, mImageView);會怎樣?這樣做意味着使用Fragment異步加載圖像。這樣做有什麼不好的影響嗎?

回答