android 图片轮播 图片切换向右滑动出现白色页面

来源:互联网 发布:幻想乡网络手游 编辑:程序博客网 时间:2024/05/05 10:13
?
public class MyViewPagerScroll extends ViewPager {
    Activity mActivity; // 上下文
    List<View> mListViews; // 图片组
    int mScrollTime = 0;
    Timer timer;
    int oldIndex = 0;
    int curIndex = 0;
    boolean listis2 = false;
    /*
     * 每隔固定时间切换广告栏图片
     */
    @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 MyViewPagerScroll(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

 
    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);
        }
        this.setAdapter(new MyPagerAdapter());// 设置适配器
        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) {
                        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);
         
            } 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 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());
        }


     


0 0
原创粉丝点击