viewpager+timer实现图片轮播 两张图片轮播时出现空白页

  @SuppressLint("HandlerLeak") private final Handler viewHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            MyViewPagerScroll.this.setCurrentItem(MyViewPagerScroll.this.getCurrentItem() + 1);
            super.handleMessage(msg);
        }

    };
    public void start(Activity mainActivity, List<View> imgList, int scrollTime,
            LinearLayout ovalLayout, int ovalLayoutId, int ovalLayoutItemId, int focusedId,
            int normalId, boolean listis2) {
        this.listis2 = listis2;
        mActivity = mainActivity;
        mListViews = imgList;
        mScrollTime = scrollTime;
        if(imgList.size()>1){
         // 设置圆点
         setOvalLayout(ovalLayout, ovalLayoutId, ovalLayoutItemId, focusedId, normalId);
        }
        pageAdapter=new MyPagerAdapter();
        this.setAdapter(pageAdapter);// 设置适配器
        if (scrollTime != 0 && mListViews.size() > 1) {
            // 设置滑动动画时间 ,如果用默认动画时间可不用 ,反射技术实现
            // new FixedSpeedScroller(mActivity).setDuration(this, 700);
            startTimer();
            // 触摸时停止滚动
             this.setOnTouchListener(new OnTouchListener() {
             public boolean onTouch(View v, MotionEvent event) {
             if (event.getAction() == MotionEvent.ACTION_UP) {
             LogUtils.e("hahahha", pageAdapter.getCount()+"==xixi======="+MyViewPagerScroll.this.getCurrentItem());
             MyViewPagerScroll.this.setCurrentItem(MyViewPagerScroll.this.getCurrentItem());
             startTimer();
             } else {
             stopTimer();
             }
             return false;
             }
             });
        }
    }
    // 设置圆点
    private void setOvalLayout(final LinearLayout ovalLayout, int ovalLayoutId,
            final int ovalLayoutItemId, final int focusedId, final int normalId) {
        if (ovalLayout != null) {
            LayoutInflater inflater = LayoutInflater.from(mActivity);
            if (listis2) {
                for (int i = 0; i < mListViews.size() - 2; i++) {
                    ovalLayout.addView(inflater.inflate(ovalLayoutId, null));
                }
                // 选中第一个
                ovalLayout.getChildAt(0).findViewById(ovalLayoutItemId).setBackgroundResource(
                    focusedId);
                this.setOnPageChangeListener(new OnPageChangeListener() {
                    public void onPageSelected(int i) {
                        curIndex = i % mListViews.size();
                        switch (curIndex) {
                            case 2:
                                curIndex = 0;
                                break;
                            case 3:
                                curIndex = 1;
                            default:
                                break;
                        }
                        // 取消圆点选中
                        ovalLayout.getChildAt(oldIndex).findViewById(ovalLayoutItemId).setBackgroundResource(
                            normalId);
                        // 圆点选中
                        ovalLayout.getChildAt(curIndex).findViewById(ovalLayoutItemId).setBackgroundResource(
                            focusedId);
                        oldIndex = curIndex;
                    }

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

                    public void onPageScrollStateChanged(int arg0) {
                    }
                });
            } else {
                for (int i = 0; i < mListViews.size(); i++) {
                    ovalLayout.addView(inflater.inflate(ovalLayoutId, null));
                }
                // 选中第一个
                ovalLayout.getChildAt(0).findViewById(ovalLayoutItemId).setBackgroundResource(
                    focusedId);
                this.setOnPageChangeListener(new OnPageChangeListener() {
                    public void onPageSelected(int i) {
                        curIndex = i % mListViews.size();
                        // 取消圆点选中
                        ovalLayout.getChildAt(oldIndex).findViewById(ovalLayoutItemId).setBackgroundResource(
                            normalId);
                        // 圆点选中
                        ovalLayout.getChildAt(curIndex).findViewById(ovalLayoutItemId).setBackgroundResource(
                            focusedId);
                        oldIndex = curIndex;
                    }

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

                    public void onPageScrollStateChanged(int arg0) {
                    }
                });
            }
        }
    }
    /**
     * 取得当明选中下标
     * 
     * @return
     */
    public int getCurIndex() {
        return curIndex;
    }

    /**
     * 停止滚动
     */
    public void stopTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    /**
     * 开始滚动
     */
    public void startTimer() {
        if (timer == null) {
            timer = new Timer();
            timer.schedule(new TimerTask() {
                public void run() {
                    mActivity.runOnUiThread(new Runnable() {
                        public void run() {
                            viewHandler.sendEmptyMessage(1);
                        }
                    });
                }
            }, mScrollTime, mScrollTime);
        }
    }

    // 适配器 //循环设置
    private class MyPagerAdapter extends PagerAdapter {
        public void finishUpdate(View arg0) {
        }

        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
        }

        public int getCount() {
            if (mListViews.size() == 1) {// 一张图片时不用流动
                return mListViews.size();
            }
            return Integer.MAX_VALUE;
        }

        public Object instantiateItem(View v, int i) {
            ((ViewPager) v).removeView(mListViews.get(i % mListViews.size()));
            ((ViewPager) v).addView(mListViews.get(i % mListViews.size()), 0);
            return mListViews.get(i % mListViews.size());
        }

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

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

        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        public Parcelable saveState() {
            return null;
        }

        public void startUpdate(View arg0) {
        }
        
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值