实现ViewPager滑动
(滑动但不循环)
1、创建一个Activity类
2、创建一个布局,里面有个ViewPager组件
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
3、(一、得到组件)在Activity类里实例化组件,并引用上述布局
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager_layout);
mViewPager = (ViewPager) findViewById(R.id.viewPager);
4、(二、建立数据源)创建多个布局(ViewPager里的每一页的布局),在Activity里得到布局,并放入一个数列集合
View v1 = LayoutInflater.from(this).inflate(R.layout.activity_viewpager_item1_layout, null);
View v2 = LayoutInflater.from(this).inflate(R.layout.activity_viewpager_item2_layout, null);
View v3 = LayoutInflater.from(this).inflate(R.layout.activity_viewpager_item3_layout, null);
ArrayList<View> arrayList = new ArrayList<View>();
arrayList.add(v1);
arrayList.add(v2);
arrayList.add(v3);
5、(三、创建适配器)
//滑动不循环
public class MyViewPagerAdapter2 extends PagerAdapter{
private ArrayList<View> arrayList = new ArrayList<View>();
private LayoutInflater layoutInflater;
public MyViewPagerAdapter2(Context context){
layoutInflater = LayoutInflater.from(context);
}
public void setData(ArrayList<View> arrayList){
this.arrayList = arrayList;
notifyDataSetChanged();
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override//实例一个View
public Object instantiateItem(ViewGroup container, int position) {
View v = arrayList.get(position);
container.addView(v);
return v;
}
@Override//销毁一个View
public void destroyItem(ViewGroup container, int position, Object object) {
View v = arrayList.get(position);
container.removeView(v);
}
}
6、ViewPager和适配器关联起来,并把数据源设置进去
MyViewPagerAdapter2 adapter = new MyViewPagerAdapter2(this);
adapter.setData(arrayList);
mViewPager.setAdapter(adapter);
(滑动循环)
1、在上述的Activity类的onCreate()方法中添加
int n = Integer.MAX_VALUE / 2 % arrayList.size();
int itemPosition = Integer.MAX_VALUE / 2 -n;
mViewPager.setCurrentItem(itemPosition);
2把适配器类换成下列这种
//滑动循环
public class MyViewPagerAdapter1 extends PagerAdapter{
private ArrayList<View> arrayList = new ArrayList<View>();
private LayoutInflater layoutInflater;
public MyViewPagerAdapter1(Context context){
layoutInflater = LayoutInflater.from(context);
}
public void setData(ArrayList<View> arrayList){
this.arrayList = arrayList;
notifyDataSetChanged();
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override//实例一个View
public Object instantiateItem(ViewGroup container, int position) {
position = position % arrayList.size();
View v = arrayList.get(position);
if (v.getParent() != null) {
container.removeView(v);
}
container.addView(v);
return v;
}
@Override//销毁一个View
public void destroyItem(ViewGroup container, int position, Object object) {
}
}
ViewPager组件的监听事件
//监听页面滚动组件
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override//当前页面
public void onPageSelected(int arg0) {
}
@Override//页面正在滚动
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override//页面改变
public void onPageScrollStateChanged(int arg0) {
}
});
(自动循环滑动)
1、先按照上述写一个可以循环滑动的ViewPager
2、创建一个Handler消息机制来发送、处理消息
private static final int TITME = 2000; /** 循环时间 */
private static final int MSG_NEXT_IMAGE = 1;/** 下一张图片 */
private static final int MSG_PAGE_CHANGED = 2;/** 设置当前页号 */
private Handler mPagerHandler = new Handler() {
boolean flag = true;
int currentPagerNo = 0; // 表示页号
public void handleMessage(android.os.Message msg) {
// 检查消息队列并移除未发送的消息,这主要是避免在复杂环境下消息出现重复等问题。
// if (mPagerHandler.hasMessages(MSG_NEXT_IMAGE) && !flag) {
// mPagerHandler.removeMessages(MSG_NEXT_IMAGE);
// flag = false;
// }
switch (msg.what) {
case MSG_NEXT_IMAGE:
currentPagerNo++;
mViewPager.setCurrentItem(currentPagerNo);
mPagerHandler.sendEmptyMessageDelayed(MSG_NEXT_IMAGE, TITME);
break;
case MSG_PAGE_CHANGED:
// 记录当前的页号,避免播放的时候页面显示不正确。
currentPagerNo = msg.arg1;
break;
}
}
};
3、在onCreate()方法中发送消息
mPagerHandler.sendEmptyMessageDelayed(MSG_NEXT_IMAGE, TITME);
4、监听ViewPager
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int postion) {
mPagerHandler.sendMessage(Message.obtain(mPagerHandler,MSG_PAGE_CHANGED, postion, 0));
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});