我正在使用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;
}
谢谢&amp;此致
答案 0 :(得分:3)
您可能需要查看InfiniteViewPager。
答案 1 :(得分:1)
答案 2 :(得分: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);
}