1

我正在使用 viewpager 从左到右和从右到左滑动图像,它工作得很好。但是,当向右滑动显示最后一张图像时,什么也没有发生。我希望在显示最后一张图像时向右滑动第一张图像..同样我希望在显示第一张图像时向左滑动最后一张图像...

将等待一些线索..

我正在使用以下代码:

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;

}

感谢和问候

4

3 回答 3

3

您可能想看看InfiniteViewPager

于 2013-05-31T06:17:00.937 回答
1

检查这两个 SO 答案Link1Link2,我相信您会找到解决方案。

于 2013-05-31T06:11:32.160 回答
0

对于您的 FlingAdapter 类:

  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);
    }
于 2013-05-31T06:07:06.287 回答