关于viewpager的滑动问题

今天碰到很诡异的问题,viewpager中放置至少三张图片的时候能够正常实现循环滑动,只放置一张或者两张的时候就不行。

后来发现问题症结:viewpager需要保证既可以向左滑动,又可以向右滑动,因此当前显示view的左侧和右侧都是存在的,只有超过这三个范围之外的才对其调用destory方法。

循环实现的代码如下:

package com.wot.cloudsensing.carrotfarm.adapter;

import java.util.ArrayList;

import com.wot.cloudsensing.carrotfarm.R;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

public class ViewPagerAdapter extends PagerAdapter {
    private ArrayList<ImageView> imageviews;
    private int size = 0;

    public ViewPagerAdapter(ArrayList<ImageView> views) {
        this.imageviews = views;
        this.size = views.size();

    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        System.out.println("111  destroyItem position = "
                + (position % imageviews.size()));
        ((ViewPager) container).removeView(imageviews.get(position
                % imageviews.size()));
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        System.out.println("111  instantiateItem instantiateItem  position = "
                + (position % imageviews.size()));
        ((ViewPager) container).addView(
                imageviews.get(position % imageviews.size()), 0);
        return imageviews.get(position % imageviews.size());
    }

}

 

posted @ 2013-09-06 17:30  bobo的学习笔记  阅读(395)  评论(0编辑  收藏  举报