package com.example.aa;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
/**
* Created by lenovo on 2017/1/25.
*/
public class ViewPagerActivity extends AppCompatActivity{
private ViewPager mBanner;
private int ImageSize = 3;
private int mImagesSrc[] = {R.drawable.guide1,R.drawable.guide2,R.drawable.guide3};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager);
ImageSize = mImagesSrc.length;
mBanner = (ViewPager) findViewById(R.id.viewpager);
mBanner.setAdapter(new BannerAdapter(this));
}
private class BannerAdapter extends PagerAdapter{
private LayoutInflater mInflater;
public BannerAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return ImageSize+2;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
/**
* 这个position是当前页面的下一个页面的缓存,若是想做滑动,position=当前item-1反之则加一
* 举个例子这个viewpager总共包含了imagesize+2=7个页面
* positio=5时 position % ImageSize=0,切换到了第一个position
*/
position %= ImageSize;
View view = mInflater.inflate(R.layout.item, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.meizi);
imageView.setImageResource(mImagesSrc[position]);
final int pos = position;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ViewPagerActivity.this, "点击item :" + pos, Toast.LENGTH_SHORT).show();
}
});
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
Log.d("", "destroyItem%" + mBanner.getCurrentItem() + "sssss" + position);
container.removeView((View) object);
}
@Override
public void finishUpdate(ViewGroup container) {
int position = mBanner.getCurrentItem();
/**
* 第五这里获得当前的positon然后对其setCurrentItem进行变换
* 这里设置当position=0时把position设置为图片列表的最大值
* 是为了position=0时左滑显示最后一张,我举个例子这里ImageSize是5
* 当position==0时设置为5,左滑就是position=4,也就是第五张图片,
*
* if (position == (ImageSize+2) - 1)
* 这个判断 (ImageSize+2)这个是给viewpager设置的页面数,这里是7
* 当position==7-1=6时,这时viewpager就滑到头了,所以把currentItem设置为1
* 这里设置为1还是为了能够左滑,这时左滑position=0又执行了第一个判断又设置为5,
* 这样就实现了无限轮播的效果
* setCurrentItem(position,false);
* 这里第二个参数false是消除viewpager设置item时的滑动动画,不理解的去掉它运行下就知道啥意思了
*
* 一进来时,position=0,走第一个if(position=3);
* 左滑:2——1——0(3)——2——1——0(3)...
* 右滑:4(1)——2——3——4(1)...
*
*/
Log.i("", "finishUpdate: "+position);
if (position == 0) {
Log.i("", "position: "+position);
position = ImageSize;
mBanner.setCurrentItem(position,false);
} else if (position == (ImageSize+2) - 1) {
Log.i("", "position: "+position);
position = 1;
mBanner.setCurrentItem(position,false);
}
}
}
}
参考:http://blog.csdn.net/verzqli/article/details/54620327
网友评论