banner的自动切换是利用hanndler,实现延迟执行,无线调用某个延迟执行的方法,实现一直循环执行延迟播放方法,同时,当点击的时候也需要把自动切换关闭,松开以后重新自动打开。需要注意的是ViewPager触摸时间的返回值是false,不拦截事件。
package com.ldw.market.holder;
import java.util.LinkedList;
import java.util.List;
import com.ldw.market.R;
import com.ldw.market.http.HttpHelper;
import com.ldw.market.utils.UiUtils;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
public class HomePictureHolder extends BaseHolder<List<String>> {
//holder创建的时候调用下面的方法
private ViewPager viewPager;
private List<String> datas;
@Override
public View initView() {
viewPager=new ViewPager(UiUtils.getContext());
//给ViewPager设置布局的参数LayoutParames,宽度设置在res/values/demons.xml中
viewPager.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT,UiUtils.getDimens(R.dimen.banner_picture_height)));
return viewPager;
}
//holder中设置数据调用setData的掉用
@Override
public void refreshView(List<String> datas) {
this.datas = datas;
viewPager.setAdapter(new HomeAdapter());
//当前条目的设置,实现“无限”滑动
viewPager.setCurrentItem(1000*datas.size());
/*
runTask = new AuToRunTask();
runTask.start();
*/
//当ViewPager按下的时候取消自动切换
viewPager.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
runTask.stop();
break;
case MotionEvent.ACTION_CANCEL: // 事件的取消
case MotionEvent.ACTION_UP:
runTask.start();
break;
}
return false; // viewPager 触摸事件 返回值要是false
}
});
runTask = new AuToRunTask();
runTask.start();
}
boolean flag;
private AuToRunTask runTask;
//banner自动切换
public class AuToRunTask implements Runnable{
@Override
public void run() {
if(flag){
//先取消之前的任务
UiUtils.cancel(this);
// 获取到ViewPager当前的条目
int currentItem = viewPager.getCurrentItem();
currentItem ++;
viewPager.setCurrentItem(currentItem);
// 延迟执行当前的任务
UiUtils.postDelayed(this, 3000);
}
}
//开始执行循环
public void start(){
//如果之前的停止的
if(!flag){
UiUtils.cancel(this);
flag = true;
//递归调用
UiUtils.postDelayed(this, 3000);
}
}
//停止
public void stop(){
if(flag){
flag=false;
UiUtils.cancel(this);
}
}
}
class HomeAdapter extends PagerAdapter{
//管理ImageView避免每次都new ImageView().LinkedList增删快速
LinkedList<ImageView> convertView = new LinkedList<ImageView>();
@Override
public int getCount() {
// 数目设置到最大,实现循环
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//uper.destroyItem(container, position, object);
//移除的对象转化成ImageView对象
ImageView view = (ImageView) object;
//把移除的对象添加到缓存的集合中
convertView.add(view);
container.removeView((View)object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
int index = position%datas.size();
ImageView view;
//如果缓存的集合不是空的就从集合中获取
if(convertView.size() > 0){
//获取到集合的第一个元素
view = convertView.remove(0);
}else{
view = new ImageView(UiUtils.getContext());
}
//初始化imageView
bitmapUtils.display(view, HttpHelper.URL+"image?name="+datas.get(index));
container.addView(view); //加载的view对象
return view; // 返回的对象
}
}
}
UiUtils.java
package com.ldw.market.utils;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.View;
import com.ldw.market.BaseApplication;
/*
* 工具类,加载字符串
*/
public class UiUtils {
/*
* 获取到字符数组 ,加载XML文件中的字符数组
* @param tabNames 字符数组的id
*/
public static String[] getStringArray(int tabNames) {
return getResource().getStringArray(tabNames);
}
public static Resources getResource() {
//获取资源
return BaseApplication.getApplication().getResources();
}
public static Context getContext(){
//获取到上下文
return BaseApplication.getApplication();
}
/** dip转换px */
public static int dip2px(int dip) {
final float scale = getResource().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f);
}
/** pxz转换dip */
public static int px2dip(int px) {
final float scale = getResource().getDisplayMetrics().density;
return (int) (px / scale + 0.5f);
}
//获取到res/values/demons.xml的配置参数
public static int getDimens(int homePictureHeight) {
return (int) getResource().getDimension(homePictureHeight);
}
/**
* 把Runnable 方法提交到主线程运行
* @param runnable
*/
public static void runOnUiThread(Runnable runnable) {
// 在主线程运行
if(android.os.Process.myTid()==BaseApplication.getMainTid()){
runnable.run();
}else{
//获取主线程的handler
BaseApplication.getHandler().post(runnable);
}
}
public static View inflate(int id) {
return View.inflate(getContext(), id, null);
}
public static Drawable getDrawalbe(int id) {
return getResource().getDrawable(id);
}
//延迟time时间执行task
public static void postDelayed(Runnable run, int time) {
// 获取到主线程的handler
BaseApplication.getHandler().postDelayed(run, time);//调用runnable中的run方法
}
//取消一个任务
public static void cancel(Runnable run) {
// 获取到主线程的handler
BaseApplication.getHandler().removeCallbacks(run);
}
}