1

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

VansFannel
  • 41,682
  • 96
  • 329
  • 561
Sudipta Som
  • 6,277
  • 9
  • 40
  • 65

3 Answers3

3

You may want to have a look at InfiniteViewPager.

MaciejGórski
  • 21,800
  • 7
  • 67
  • 92
1

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

Community
  • 1
  • 1
Evos
  • 3,905
  • 2
  • 16
  • 21
0

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);
    }
Rodion Altshuler
  • 1,583
  • 14
  • 29