Question

Myself trying to load the images(from webservice) in gridview and it was succesfully done. I used Lazy Adapter to load all images from web service.

If i click on some image it should be displayed in next Activity and while swiping that image next image should be displayed. Any Ideas???

Was it helpful?

Solution

This can be relatively easy realised using a ViewPager, I'd say. Consider the flow as follows: When the user selects a list item, pass on a list of all the images (e.g. their URIs) and an index indicating the one selected to the activity containing the ViewPager. You can then initialise the ViewPager's adapter with the passed on list and set the current item to display to the index.

Have a look at the API demos for some more hints on how to use a ViewPager, or read the blog post on developers.android.com. In stead of feeding Fragments as Views to the ViewPager, simply instantiate an ImageView - you may find this Q/A on SO useful for some pointers on how to do that. Also, probably equally important to read through is the documentation on PagerAdapter.

//Edit: Some pointers for coding up above suggestion:

Have your onItemClick create a new Intent, add the relevant data for retrieving the images as extra as well as the selected index and start the Activity:

@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(this, ImageViewer.class);
    intent.putExtra(PAGE_POSITION, position);
    intent.putStringArrayListExtra(IMAGE_LIST, mImages)
    // or add a serializable, e.g. an ArrayList<T> with your POJOs
    intent.putExtra(IMAGE_LIST, mImages);
    startActivity(intent);
}

In your ImageViewer Activity containing the ViewPager, retrieve all the extras and initialise the ViewPager to the given position/index:

if (getIntent() != null && getIntent().getExtras() != null {
    mImagePosition = getIntent().getExtras().getInt(PAGE_POSITION, 0);
    mImageList = getIntent().getExtras().getStringArrayList(IMAGE_LIST);
    // or if you used Serializables
    mImageList = (ArrayList<T>) getIntent().getExtras().getSerializable(IMAGE_LIST);
    mViewPager.setAdapter(new ImagePagerAdapter());
    mViewPager.setCurrentItem(mImageIndex);
}

In the ImagePaderAdapter you instantiate the ImageViews containing the images you want to display. There are tons of examples out there, but it'll look something like this in the basis:

private class ImagePagerAdapter extends PagerAdapter {

    public int getCount() {
        return mImageList == null ? 0 : mImageList.size();
    }

    public Object instantiateItem(View pager, int position) {
        if (mInflater == null) mInflater = (LayoutInflater) container.getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = mInflater.inflate(R.layout.image_pager_item_layout, null, false);
        ImageView photoImageView = (ImageView) view.findViewById(R.id.photo_imageview);

        mImageLoader().loadImage(mImageList.get(position), photoImageView);

        ((ViewPager) pager).addView(view, 0);

        return view;
    }       

    public void destroyItem(View pager, int position, Object object) { 
        ((ViewPager) pager).removeView((View) object);
    }

    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    public void finishUpdate(View container) { }
    public void restoreState(Parcelable state, ClassLoader loader) { }
    public void startUpdate(View container) { }
    public Parcelable saveState() { return null; }
}

Note that I have not tested the code above, nor is it meant to be complete. For instance, I assume you know how to get a reference to a LayoutInflater yourself and have a 'lazy' image loader that asynchronously sets an image against an ImageView. The image_pager_item_layout file can simply be an ImageView, but also a more complex hierarchy of views inside a ViewGroup (like with ListView and GridView), e.g. you can easily add an caption to images. It's quite similar to

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top