Android 使用Handler的PostDelayed方法实现图片的轮播

第一步:创建MainActivity类

    public class BaseActivity extends ActionBarActivity {
        private List<String> mDatas;
        private ViewPager mViewPager;
        private boolean isRun = false;
        private AutoRunTask mTask;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mViewPager = new ViewPager(this);
            mViewPager.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, Util
                .getDimen(R.dimen.home_pic_height)));
            mViewPager.setAdapter(new HomePagerAdapter());
            // 设置滑动的初始位置
            mViewPager.setCurrentItem(1000 * mDatas.size());
            mViewPager.setOnTouchListener(this);

            mTask = new AutoRunTask();
            mTask.start();
        }
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTask.stop();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mTask.start();
                break;

            default:
                break;
            }
            return false; //viewpager必须是false,如果是true是就不能移动
        }
   }

第二步:创建无限循环的PagerAdaper

class HomePagerAdapter extends PagerAdapter {
        // 使用此对imageView进行复用
        private LinkedList<ImageView> convertViews = new LinkedList<ImageView>();

        @Override
        public int getCount() {
            // return mPicUrls.size();
            return Integer.MAX_VALUE; // 可以无线滑动
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            if (object != null && object instanceof ImageView) {
                ImageView imageView = (ImageView) object;
                // 添加到集合里
                convertViews.add(imageView);
                container.removeView(imageView);
            }
            super.destroyItem(container, position, object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // position 因为无线大,所以
            int index = position % mDatas.size();

            ImageView imageView = null;
            if (convertViews != null && convertViews.size() > 0) {
                // 取出一个imageView
                imageView = convertViews.remove(0);
            } else {
                imageView = new ImageView(Util.getContext());
            }

            String uri = HttpHelper.URL + "image?name=" + mDatas.get(index);
            mBitmapUtils.display(imageView, uri);
            container.addView(imageView);
            return imageView;
        }

    }

第三步:使用handler创建轮播类

public class AutoRunTask extends Handler implements Runnable {
    @Override
    public void run() {
        while (isRun) {
            removeCallbacks(this);
            int currentItem = mViewPager.getCurrentItem();
            currentItem++;
            mViewPager.setCurrentItem(currentItem);
            postDelayed(this, 2000);
        }
    }
    public void start() {
        if (!isRun) {
            removeCallbacks(this);
            isRun = true;
            postDelayed(this, 2000);
        }
    }

    public void stop() {
        if (isRun) {
            isRun = false;
            removeCallbacks(this);
        }
    }
}

你可能感兴趣的:(android,handler,message,轮播)