Sudipta Som
Sudipta Som

Reputation: 6577

Image slide using viewpager in Android

I am using viewpager to slide images left to right and right to left, and its working very well. But when the last image comes to display on swiping to the right nothing happens. I want the first image on swiping to the right when last image is displayed..and similarly i want last image on swiping to the left when first image is displayed...

Will wait for some clue..

I am using following code:

public class PhotoFullActivity extends Activity{

private FlingViewPager viewPager;
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);     
    viewPager = new FlingViewPager(this);
    setContentView(viewPager);

            FlingAdapter adapter = new FlingAdapter(this, photoArrayList); //photoArrayList is the collection of phot path from SD card
    viewPager.setAdapter(adapter);
    }

=================================================================

    public class FlingViewPager extends ViewPager {

    public FlingViewPager(Context context) {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        try {
            return super.onInterceptTouchEvent(ev);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return false;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}

===========================================================

public class FlingAdapter extends PagerAdapter{

private Context context;
ArrayList<String> adapterList;

public FlingAdapter(Context context,ArrayList<String> adapterList)
{
    this.context = context;
    this.adapterList = adapterList;
}


public View instantiateItem(ViewGroup container, int position) {


    PhotoView photoView = new PhotoView(container.getContext());

    String photo_path = adapterList.get(position);
    File file = new File(photo_path);
    Bitmap bmp_thumb = BitmapFactory.decodeFile(file.getAbsolutePath());
    photoView.setImageBitmap(bmp_thumb);

    // Now just add PhotoView to ViewPager and return it
    container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

    return photoView;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return this.adapterList.size();
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
}

@Override
public boolean isViewFromObject(View view, Object object) {

    return view == object;

}

Thanks & Regards

Upvotes: 1

Views: 6543

Answers (3)

MaciejG&#243;rski
MaciejG&#243;rski

Reputation: 22232

You may want to have a look at InfiniteViewPager.

Upvotes: 3

Evos
Evos

Reputation: 3915

Chech this two SO answers Link1 and Link2 and i'm sure you will find a solution.

Upvotes: 1

Rodion Altshuler
Rodion Altshuler

Reputation: 1783

For your FlingAdapter class:

  int count;

    @Override
    public int getCount() {
        //actually, not complete solution, but hope the idea would help you
        //you could increase "count" variable in getView() when you're reaching bounds of array

        count = this.adapterList.size()*2;
        return count;
    }

    @Override
    public <String> getItem(int position){
        //for example, size of array is 5, so last index is 5-1=4;
        //with position == 5, we should return index 0
        int dataPosition = (position % (this.adapterList.size()-1))-1;
        return this.adapterList.get(dataPosition);
    }

Upvotes: 0

Related Questions