使用viewPager进行幻灯片放映

时间:2018-04-09 06:21:34

标签: android android-viewpager slider viewpagerindicator autoscroll

我尝试使用slide-show创建viewPager。我的问题是slide-show的第一项在大约3秒钟后即使使用Picasso库也会显示,并且图像也存储在本地,我在3台真实设备上测试过,结果是一样的,如何我应该解决这个问题?

活动代码:

private void slider() {

   CircleIndicator indicator = (CircleIndicator) findViewById(R.id.ma_slide_indicator);

    sData=new ArrayList<>();

    sData.add(new Slider_Model("item 1",31235545,R.drawable.angrybirds));
    sData.add(new Slider_Model("item 2",12225545,R.drawable.asphalt8));
    sData.add(new Slider_Model("item 3",45522545,R.drawable.clashofclans));
    sData.add(new Slider_Model("item 4",71225222,R.drawable.fruitninja));
    sData.add(new Slider_Model("item 5",91000220,R.drawable.talkingtom));
    sData.add(new Slider_Model("item 6",100022002,R.drawable.cuttherope));
    sData.add(new Slider_Model("item 7",5555445,R.drawable.pou));

    Slide_Adapter slide_adapter = new Slide_Adapter(this,sData);
    viewPager.setAdapter(slide_adapter);
    viewPager.setOffscreenPageLimit(2);
    indicator.setViewPager(viewPager);

    NUM_PAGES=sData.size();

     Runnable Update = new Runnable() {
        public void run() {
            if (currentPage == NUM_PAGES) {
                currentPage = 0;
            }
            viewPager.setCurrentItem(currentPage++, true);
        }
    };

   timing(new Handler(),Update,4000,4000);


    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            Log.v(TAG, "SCROLL POS " + position);
            currentPage = position;
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

}

private void timing(final Handler h, final Runnable run, int delay, int period){

    Timer swipeTimer = new Timer();
    swipeTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            h.post(run);
        }
    }, delay, period);
}

和适配器:

public class Slide_Adapter extends PagerAdapter {


private static final String TAG ="value" ;
private Context mContext;
private LayoutInflater Inflater;
private ArrayList<Slider_Model> sData;



public Slide_Adapter(Context mContext, ArrayList<Slider_Model> data) {
    this.mContext = mContext;
    this.Inflater=LayoutInflater.from(mContext);
    this.sData=data;
}




@Override
public boolean isViewFromObject(View view, Object object) {
   return view.equals(object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

       View v=Inflater.inflate(R.layout.slideshow_layout,container,false);



        assert v !=null;
        ImageView imageView=(ImageView) v.findViewById(R.id.slide_imageView);
        TextView title=(TextView) v.findViewById(R.id.slide_title);
        TextView phone=(TextView) v.findViewById(R.id.slide_ph);

        title.setText(sData.get(position).getTitle());
        phone.setText(String.valueOf(sData.get(position).getPhone()));
        Picasso.get().load(sData.get(position).getImage()).into(imageView);



        container.addView(v,0);

    return v;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {

    container.removeView((View) object);

}
@Override
public int getCount() {
    return sData.size();
}

}

1 个答案:

答案 0 :(得分:0)

由于我们的currentPage是全局变量,因此请确保将其正确初始化为零。然后你需要在启动计时器之前设置当前项目:

...
viewPager.setCurrentItem(currentPage, true);
Runnable Update = new Runnable() {
    public void run() {
        if (currentPage == NUM_PAGES) {
            currentPage = 0;
        }
        viewPager.setCurrentItem(currentPage++, true);
    }
};
...