一、效果如图所示:
二、实现步骤:
1、布局和准备工作;
2、改变页面描述文字;
3、动态添加指示点;
4、实现左右无限循环;
5、实现页面自动切换;
三、具体实现:
1、Activity布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="200dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/vp"
android:background="#99000000"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是描述信息"
android:textColor="#FFFFFF" />
<LinearLayout
android:id="@+id/ll_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp" />
</LinearLayout>
</RelativeLayout>
2、小圆点的实现
红色小圆点
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:height="5dp"
android:width="5dp" />
<solid android:color="#ff0000" />
</shape>
白色小圆点
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:height="5dp"
android:width="5dp" />
<solid android:color="#ffffff" />
</shape>
3、代码实现
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private int flush = 1;
private TextView tv;
private ViewPager vp;
private LinearLayout ll_point;
private boolean isRunning;
//页面改变时上一个页面的Position
private int lastPointPosition;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (isRunning) {
//ViewPager切换至下一页
vp.setCurrentItem(vp.getCurrentItem() + 1);
//再次发送延迟消息
handler.sendEmptyMessageDelayed(flush, 2000);
}
}
};
//ViewPager的页面改变监听事件
private final ViewPager.OnPageChangeListener myPageChangeListener
= new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
position = position % imageIds.length;
//设置当前页面描述文字
tv.setText(tvDesc[position]);
//将当前页面的小圆点置为红色
ll_point.getChildAt(position).setBackgroundResource(
R.drawable.shape_point_red
);
//将上一个页面的小圆点置为白色
ll_point.getChildAt(lastPointPosition).setBackgroundResource(
R.drawable.shape_point_white
);
//将当前页的position赋值给lastPointPosition,方便下次使用
lastPointPosition = position;
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
//ViewPager适配器数据源图片的资源Ids
private int[] imageIds = {R.mipmap.a, R.mipmap.b, R.mipmap.c,
R.mipmap.d, R.mipmap.e};
//ViewPager适配器的数据源
private List<ImageView> imageViews;
//tv的描述文字集合
private String[] tvDesc = {"巩俐不低俗,我就不能低俗",
"朴树又回来了!再唱经典引万人大合唱!",
"揭秘北京电影如何升级", "乐视TV超级大放送",
"热血屌丝的反杀"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = (ViewPager) findViewById(R.id.vp);
tv = (TextView) findViewById(R.id.tv);
ll_point = (LinearLayout) findViewById(R.id.ll_point);
//准备ViewPager适配器的数据源
imageViews = new ArrayList<>();
for (int i = 0; i < imageIds.length; i++) {
ImageView iv = new ImageView(this);
iv.setBackgroundResource(imageIds[i]);
//将ImageView添加到ViewPager适配器数据源的集合
imageViews.add(iv);
//动态添加指示点
ImageView point = new ImageView(this);
//默认让第一个页面的小圆点置为红色,其他的置为白色
if (i == 0) {
point.setBackgroundResource(R.drawable.shape_point_red);
} else {
point.setBackgroundResource(R.drawable.shape_point_white);
}
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = 15;
ll_point.addView(point, params);
}
//tv设置默认描述文字
tv.setText("巩俐不低俗,我就不能低俗");
//设置ViewPager的适配器
vp.setAdapter(new MyPagerAdapter());
//让ViewPager直接显示指定下标的页面
//页面的下标值足够大,并且是实际页面个数的整数倍
int destItem = Integer.MAX_VALUE / 2 -Integer.MAX_VALUE / 2 % imageIds.length;
vp.setCurrentItem(destItem);
//给ViewPager设置页面改变监听
vp.addOnPageChangeListener(myPageChangeListener);
//使用Handler发送延迟消息,每隔2s自动切换至下一页
handler.sendEmptyMessageDelayed(flush, 2000);
isRunning = true;
}
@Override
protected void onDestroy() {
super.onDestroy();
//当前Activity销毁时,令自动轮播不再工作
isRunning = false;
}
private class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
//return imageViews.size();
//使ViewPager的边界足够大,以实现无限轮播
return Integer.MAX_VALUE;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
//return super.instantiateItem(container, position);
//以防出现OutOfIndex异常
position = position % imageIds.length;
ImageView iv = imageViews.get(position);
container.addView(iv);
return iv;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//super.destroyItem(container, position, object);
//上面这句话必须删掉,否则程序就会崩掉,通过查看源码得知,这句话默认会抛出一个异常
container.removeView((View) object);
}
}
}