简体   繁体   中英

android using ViewPager to show images

I'd like to download a bunch of images from web and then show them using my custom PagerAdapter. So I can slide to browse all of these images.

I define an activity named as PhotoSlideActivity which extends from FragmentActivity . In this activity, I have my own PagerAdapter . The following is my code:

    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;
        }
    }

The following is my PhotoSlidePageFragment . mImageFetcher is a class which download images asynchronously.

   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) My original idea is to put mImageFetcher.loadImage(mImageUrl, mImageView); in onCreateView . Is there any difference between putting it in onCreateView and in onActivityCreated ?

(2) What if I replace what I did in onActivityCreated with mImageFetcher = new ImageFetcher(); mImageFetcher.loadImage(mImageUrl, mImageView); mImageFetcher = new ImageFetcher(); mImageFetcher.loadImage(mImageUrl, mImageView); ? Doing this means use Fragment to load the image asynchronously. Is there any bad effects doing this?

Use can use LazyList for fetch data from the website(URL). download following example from Github

https://github.com/thest1/LazyList

and adapter data use in swipe change image example

https://github.com/chiuki/android-swipe-image-viewer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM