public class PicturePagerAdapter extends PagerAdapter {
private static final String tag = "PicturePagerAdapter";
List<ImageView> list;
public PicturePagerAdapter(List<ImageView> imageViews) {
this.list = imageViews;
}
@Override
public int getCount() {
// 确定要显示的数据量
return Integer.MAX_VALUE;// 无限轮播
}
@Override
public boolean isViewFromObject(View view, Object object) {
// 判断当前要显示的view是否等于标记object,true则显示
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
position = position % list.size();
Log.d(tag, "instantiateItem==" + position);
// 无限轮播-因为position的值从0~Integer.MAX_VALUE
View view = list.get(position);
// 添加view到VIewPager中
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// 从容器中移除view
Log.d(tag, "destroyItem==" + position);
container.removeView((View) object);
}
}
public class AutoShowView extends FrameLayout {
private View view;
private ViewPager vp_lunbo;
private TextView tv_desc;
private LinearLayout ll_points;
private List<ImageView> datas;
private PicturePagerAdapter adapter;
private Handler handler;
final AutoScrollTask autoScrollTask = new AutoScrollTask();
protected int selectIndex;
public AutoShowView(Context context) {
this(context, null);
}
public AutoShowView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AutoShowView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
initData();
initListener();
}
private void initView(Context context) {
view = LayoutInflater.from(context).inflate(R.layout.view_autoshow,
this);
vp_lunbo = (ViewPager) view.findViewById(R.id.vp_lunbo);
tv_desc = (TextView) view.findViewById(R.id.tv_desc);
ll_points = (LinearLayout) view.findViewById(R.id.ll_points);
datas = new ArrayList<ImageView>();
adapter = new PicturePagerAdapter(datas);
if (handler == null) {
handler = new Handler();
}
}
private void initData() {
// 初始化图片数据
initImageView();
// 初始化点数据
initPoints();
vp_lunbo.setAdapter(adapter);
// 设置ViewPager当前选中的位置,会调用instantiateItem,最终会选中item0
// 原理:一个数减去其余数后肯定能被除数整除
vp_lunbo.setCurrentItem(10000 - 10000 % datas.size());
autoScrollTask.start();
}
private void initImageView() {
for (int i = R.drawable.icon1; i < R.drawable.icon1 + 4; i++) {
// 创建ImageView容器
ImageView imageView = new ImageView(getContext());
// 设置图片
imageView.setImageResource(i);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);// 图片填充容器
datas.add(imageView);// 添加到集合中
}
}
private void initPoints() {
// 因为每次页面改变都会调用该方法,所以进入该方法时先清空集合。
ll_points.removeAllViews();
for (int i = 0; i < datas.size(); i++) {
View view = new View(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
10, 10);
if (i == selectIndex) {
tv_desc.setText("图片" + i + "的描述");
view.setBackgroundResource(R.drawable.point_red);
} else {
view.setBackgroundResource(R.drawable.point_white);
}
params.leftMargin = 10;
view.setLayoutParams(params);// 设置布局参数
ll_points.addView(view);// 添加到点的容器中
}
}
private void initListener() {
// viewpager注册页面改变监听器
vp_lunbo.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// 页面选中调用
selectIndex = position % datas.size();
initPoints();// 红点跟着页面改变而移动
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) { // 页面滑动就调用
}
@Override
public void onPageScrollStateChanged(int state) {
// 页面状态改变调用
}
});
// viewpager 注册触摸事件监听器,实现自动轮播
vp_lunbo.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:// 按下停止轮播
autoScrollTask.stop();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:// 松开,取消 开始轮播
case MotionEvent.ACTION_CANCEL:
autoScrollTask.start();
break;
default:
break;
}
return false;// 不会影响其他view的事件分发
}
});
}
/** 实现轮播图自动轮播 */
class AutoScrollTask implements Runnable {
@Override
public void run() {
int currentItem = vp_lunbo.getCurrentItem();
currentItem++;
vp_lunbo.setCurrentItem(currentItem);
start();
}
/**
* 开始轮播
*/
public void start() {
handler.postDelayed(this, 2000);
}
/**
* 停止轮播
*/
public void stop() {
handler.removeCallbacks(this);
}
}
}
protected static final String tag = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
<com.example.lenovo.lianxi.view.AutoShowView
android:id="@+id/asv"
android:layout_width="match_parent"
android:layout_height="200dp">
</com.example.lenovo.lianxi.view.AutoShowView>
<android.support.v4.view.ViewPager
android:id="@+id/vp_lunbo"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
<!-- 文字描述和点的容器点和容器 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#22000000"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个点的描述"
android:textColor="#ffffff" />
<LinearLayout
android:id="@+id/ll_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="5dp" />
<solid android:color="#ff0000" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="5dp" />
<solid android:color="#ffffff" />
</shape>