图1和图5的是一样的
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.viewpager.ViewPagerMyAdapter.MyAdapter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MainActivity extends Activity implements ViewPager.OnPageChangeListener {
private ArrayList<ImageView> imageviewlist;
private MyAdapter myAdapter;
private ViewPager viewPager;
private ImageView imageView;
private List<View> views;
private TextView tv_title;
private int[] images = {R.mipmap.yan, R.mipmap.pl, R.mipmap.po, R.mipmap.pp, R.mipmap.yan};
private String[] titles = {"标题1", "标题2", "标题3", "标题4", "标题5"};
//记录上一点的位置
private int currentItem;
//存放图片的id
private int oldPosition = 0;
private ScheduledExecutorService scheduledExecutorService;//定时执行者服务
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initadapter();
}
private void initViews() {
viewPager = findViewById(R.id.vp);
imageviewlist = new ArrayList<>();
for (int i = 0; i < images.length; i++) {
imageView = new ImageView(this);
imageView.setBackgroundResource(images[i]);
imageviewlist.add(imageView);
}
//显示的小点
views = new ArrayList<>();
views.add(findViewById(R.id.dot_0));
views.add(findViewById(R.id.dot_1));
views.add(findViewById(R.id.dot_2));
views.add(findViewById(R.id.dot_3));
views.add(findViewById(R.id.dot_4));
tv_title = findViewById(R.id.tv_title);
tv_title.setText(titles[0]);
viewPager.setOnPageChangeListener(this);//设置滚动监听
}
private void initadapter() {
//实例化适配器
myAdapter = new MyAdapter(this, imageviewlist);
viewPager.setAdapter(myAdapter);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//页面滑动时调用该方法
}
@Override
public void onPageSelected(int position) {
//新的条目被选中时调用
// Log.i(TAG,"position2..."+position);
tv_title.setText(titles[position]);//更新标题
//更换小点的样式
views.get(position).setBackgroundResource(R.drawable.dot_focused);
views.get(oldPosition).setBackgroundResource(R.drawable.dot_normal);
currentItem = position;
oldPosition = position;
}
@Override
public void onPageScrollStateChanged(int state) {
//活动状态变化时调用
}
@Override
protected void onStart() {
super.onStart();
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
scheduledExecutorService.scheduleWithFixedDelay(
new ViewPageTask(),
2,//首张图片停留多少秒后跳转
2,//后续的图片停留多少秒后跳转
TimeUnit.SECONDS);//用时间单位计算,这里用的是秒数(SECONDS)
}
private class ViewPageTask implements Runnable {
@Override
public void run() {
currentItem = (currentItem + 1) % images.length;
//发送空的信息
mhandler.sendEmptyMessage(0);
}
}
//接收ViewPageTask消息 更新UI界面
private Handler mhandler = new Handler() {
@Override
public void handleMessage(Message msg) {
viewPager.setCurrentItem(currentItem);
}
};
}
Adapter类
public class MyAdapter extends PagerAdapter{
private List<ImageView> imglist;
private Context context;
public MyAdapter(Context context,List<ImageView> imglist) {
this.imglist=imglist;
this.context=context;
}
@Override
public int getCount() {
return imglist.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}
//移除
@Override
public void destroyItem(ViewGroup view, int position, Object object) {
view.removeView(imglist.get(position));
}
//实例化条目,添加图片
@Override
public Object instantiateItem(ViewGroup view, int position) {
view.addView(imglist.get(position));
return imglist.get(position);
}
}
//布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#66000000"
android:gravity="center">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:textColor="@android:color/white"
android:textSize="25sp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
>
<View
android:id="@+id/dot_0"
android:layout_width="5dp"
android:layout_height="5dp"
android:background="@drawable/dot_focused"
/>
<View
android:id="@+id/dot_1"
android:layout_marginLeft="5dp"
android:layout_width="5dp"
android:layout_height="5dp"
android:background="@drawable/dot_normal"
/>
<View
android:id="@+id/dot_2"
android:layout_marginLeft="5dp"
android:layout_width="5dp"
android:layout_height="5dp"
android:background="@drawable/dot_normal"
/>
<View
android:id="@+id/dot_3"
android:layout_marginLeft="5dp"
android:layout_width="5dp"
android:layout_height="5dp"
android:background="@drawable/dot_normal"
/>
<View
android:id="@+id/dot_4"
android:layout_marginLeft="5dp"
android:layout_width="5dp"
android:layout_height="5dp"
android:background="@drawable/dot_normal"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6">
</LinearLayout>
</LinearLayout>
//drawable的
//灰色小圆点
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="5dp"/>
<solid android:color="@android:color/darker_gray"/>
</shape>
//白色小圆点
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="5dp"/>
<solid android:color="#aaffffff"/>
</shape>