轮播图

首先初始化几张图片

private int[] imgs = { R.drawable.page1, R.drawable.page2, R.drawable.page3 };

    /** * 初始化首页轮播图,本地图片 */
    private void initImg() {
        imgList = new ArrayList<ImageView>();
        for (int i = 0; i < imgs.length; i++) {
            ImageView view = new ImageView(mContext);
            view.setBackgroundResource(imgs[i]);
            imgList.add(view);
        }
    }

递归方法

    private void initHandler() {
        if (mHandler == null) {
            mHandler = new InternalHandler();
        } else {
            mHandler.removeCallbacksAndMessages(null);
        }
        mHandler.postDelayed(new InternalRunnable(), 4000);
    }

viewpager设置

        mViewPager.setAdapter(new ViewPagerAdapter(imgList, mHandler));
        mViewPager.setOnPageChangeListener(new HomeOnPageChangeListener());

viewpager监听器

public class ViewPagerAdapter extends PagerAdapter {
    private List<ImageView> mImageViewList;
    private Handler mHandler;

    public ViewPagerAdapter(List<ImageView> imageViewList, Handler mHandler) {
        super();
        this.mImageViewList = imageViewList;
        this.mHandler = mHandler;

    }

    @Override
    public int getCount() {
        return mImageViewList.size();
    }

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

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView iv = mImageViewList.get(position);
        iv.setScaleType(ScaleType.FIT_XY);
        iv.setOnTouchListener(new MyOnTouchListener());
        container.addView(iv);
        return iv;
    }

    class MyOnTouchListener implements OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mHandler.removeCallbacksAndMessages(null); // 移除消息队列中所有的任务和消息.
                break;
            case MotionEvent.ACTION_CANCEL: // 事件丢失
                mHandler.postDelayed(new InternalRunnable(), 4000);
                break;
            case MotionEvent.ACTION_UP:
                mHandler.postDelayed(new InternalRunnable(), 4000);
                break;
            default:
                break;
            }

            return true;
        }
    }

    class InternalRunnable implements Runnable {

        @Override
        public void run() {
            mHandler.sendEmptyMessage(0);
        }
    }
}

viewpager滚动时监听动作

class HomeOnPageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int arg0) {
            for (int i = 0; i < imgs.length; i++) {
                llPointGroup.getChildAt(i).setEnabled(false);
            }
            llPointGroup.getChildAt(arg0).setEnabled(true);
        }

    }

递归方法

/** * 控制轮播图自动滚动 * */
    class InternalHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            int currentItem = (mViewPager.getCurrentItem() + 1)
                    % imgList.size();
            mViewPager.setCurrentItem(currentItem);
            mHandler.postDelayed(new InternalRunnable(), 4000);
        }
    }

    class InternalRunnable implements Runnable {

        @Override
        public void run() {
            mHandler.sendEmptyMessage(0);
        }
    }

你可能感兴趣的:(轮播图)