【美高梅开户网址】行使swift封装本身的轮播图控件,轮播控件

一、要做成什么

【美高梅开户网址】行使swift封装本身的轮播图控件,轮播控件。背景

方今要做七个轮播图的职能,网上看了几篇作品,基本上都能找到达成,效果还挺不错,不过在写的时候感觉每一次都要独立去重新在Activity里写一批代码。于是本身包裹了一晃。本篇轮播图完成原理原来的小说出处:循环广告位组件的完结,那里只是做了下封装成贰个控件,不必每一回重复写代码了。

背景

日前要做叁个轮播图的功能,网上看了几篇作品,基本上都能找到实现,效果还挺不错,然而在写的时候觉得每便都要单独去重新在Activity里写一群代码。于是本身包裹了须臾间。那里只是做了下封装成二个控件,不必每回重复写代码了。

就算如此网上大把包装好的轮播图控件,可是本身认为仍旧有必要自个儿写一下。因为如此也好不不难一种学习,而且自身写的事物更易于控制,用到温馨的花色里的时候更易于修改定制,也更易于精简项目代码。

  bs端的轮播控件千千万,有的仍是可以够同日而语2个单身的库来开发,所提到到的效益也是缤纷多彩。相对来说,cs端的轮播用得不多,我那边只是简短的做了个能满意壹般必要的轮播,在品种中凑会凑会依然得以的。先给两张图,看看最终的效能:

效果图

美高梅开户网址 1

效果图

美高梅开户网址 2

本人查了有的素材,达成轮播图首要依靠ScrollView大概CollectionView来落到实处,总的来说使用CollectionView能够相比较简单的做1些尤其的轮转效应,不过不荒谬的话大家用到的轮播图都是用部分很基本的意义,个人觉得使用ScrollView来做完全就能够了,那样控件的份额级会小一些。而且那里只封装基本功用,满意抢先2五%的须要,假使有亟待再去定制,那样能够使项目里没用的代码越来越少1些,那也是本身不用那个有成千上万酷炫效果,很多效果的外人封装的很好,也很好用的那三个轮播控件的根本缘由。也许是性冷淡,作者愿意笔者的品种里尽可能少的存在这么些并未有运转的代码,就算我并无法确定保障完全未有。

美高梅开户网址 3

兑现分析

轮播图的成效正是落实左右滑动的广告、图片消息展现,那我们就用ViewPager来完毕,由于思考到用户体验,大家还索要在上边加八个提示器来标示滑动到了第几张轮播图。提示器我们得以用一个线性布局来依照要展示的轮播图设置展现的View,大家要做这么的2个控件未有何样异样的成效,其实就是几个控件的结合,只是我们要在其间处理好它们中间的互相关系(其实便是ViewPager滚动的时候,下边提醒器的显得),所以大家就用自定义控件个中的组成措施来促成。 
上边初步

福衢寿车分析

轮播图的作用就是兑现左右滑动的广告、图片消息呈现,那我们就用ViewPager来达成,由于挂念到用户体验,大家还必要在底下加1个提示器来标示滑动到了第几张轮播图。提醒器我们能够用三个线性布局来依照要显得的轮播图设置呈现的View,大家要做那样的三个控件未有怎么特别的功效,其实便是多少个控件的结缘,只是大家要在里边处理好它们中间的竞相关系(其实正是ViewPager滚动的时候,上面提醒器的展现),所以大家就用自定义控件个中的三结合格局来贯彻。
上边开头

参照小说OS开发UI篇—UIScrollView控件达成图片轮播
注:此篇文章仅用于学习和保留代码,便于复用。

美高梅开户网址 4

一、定义2个控件继承FrameLayout,写八个xml文件

public class CarouselView extends FrameLayout implements ViewPager.OnPageChangeListener {

    private Context context;

    private int totalCount =100;//总数,这是为实现无限滑动设置的

    private int showCount;//要显示的轮播图数量

    private int currentPosition =0;//当前ViewPager的位置

    private ViewPager viewPager;

    private LinearLayout carouselLayout;//展示指示器的布局

    private Adapter adapter;
    private int pageItemWidth;//每个指示器的宽度
    private boolean isUserTouched = false;

    public CarouselView(Context context) {
        super(context);
        this.context = context;
    }

    public CarouselView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public CarouselView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.view.ViewPager
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:unselectedAlpha="1">
    </android.support.v4.view.ViewPager>
    <LinearLayout android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_gravity="center|bottom"
        android:id="@+id/CarouselLayoutPage"
        android:padding="10dip">
    </LinearLayout>
</FrameLayout>

上边的代码把多少个要用到的控件ViewPager和carouselLayout都含有在概念的CarouselView里面了,下边便是要收获

壹、定义一个控件继承FrameLayout,写1个xml文件

public class CarouselView extends FrameLayout implements ViewPager.OnPageChangeListener {

    private Context context;

    private int totalCount =100;//总数,这是为实现无限滑动设置的

    private int showCount;//要显示的轮播图数量

    private int currentPosition =0;//当前ViewPager的位置

    private ViewPager viewPager;

    private LinearLayout carouselLayout;//展示指示器的布局

    private Adapter adapter;
    private int pageItemWidth;//每个指示器的宽度
    private boolean isUserTouched = false;

    public CarouselView(Context context) {
        super(context);
        this.context = context;
    }

    public CarouselView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public CarouselView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.view.ViewPager
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:unselectedAlpha="1">
    </android.support.v4.view.ViewPager>
    <LinearLayout android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_gravity="center|bottom"
        android:id="@+id/CarouselLayoutPage"
        android:padding="10dip">
    </LinearLayout>
</FrameLayout>

地方的代码把多个要用到的控件ViewPager和carouselLayout都含有在概念的CarouselView里面了,下边就是要获取

下边是代码
/// 轮播图

import UIKit


class SlideImages: UIView ,UIScrollViewDelegate{
    private enum ImageType{
        case Image     //本地图片
        case URL       //URL
    }

    private var scrollView:UIScrollView?
    private var index:Int = 0{
        didSet{
            switch type {
            case .Image:
                changeLeftIndeAndRightIndeWith(imageArr)
            default:
                changeLeftIndeAndRightIndeWith(urlArr)
            }
        }
    }

    private var leftImg = UIImageView()
    private var centerImg = UIImageView()
    private var rightImg = UIImageView()
    private var pageControl = UIPageControl()
    private var timer:NSTimer?
    private var leftIndex:Int = 0
    private var rightIndex:Int = 0
    private var type:ImageType = .Image

    /// 间隔时间
    var interval:Double = 4
    /// 点击回调
    var clickBlock :(Int)->Void = {index in}
    /// url图片数组
    var urlArr = [String](){
        didSet{
            type = .URL
            setSlideImages()
        }
    }
    /// 本地图片数组
    var imageArr = [UIImage](){
        didSet{
            type = .Image
            setSlideImages()
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        setSlideImages()
    }

    //设置图片URL
    func setURLsWithArr(arr:[String],imageClickBlock:(Int) -> Void ) {
        urlArr = arr
        clickBlock = imageClickBlock
    }
    //设置图片
    func setImagesWithArr(arr:[UIImage],imageClickBlock:(Int) -> Void ) {
        imageArr = arr
        clickBlock = imageClickBlock
    }

    private func setSlideImages(){
        //关闭定时器并清除所有内容
        closeTimer()
        for view in subviews {
            view.removeFromSuperview()
        }

        if urlArr.count == 0 && imageArr.count == 0{
            return
        }

        if urlArr.count == 1 || imageArr.count == 1{//只有一张图片不用滚动
            centerImg = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
            centerImg.userInteractionEnabled = true
            addTapGesWithImage(centerImg)

            switch type {
            case .Image:
                centerImg.image = imageArr[0]
            default:
                centerImg.setMyImageWithURL(NSURL(string: urlArr[0]), placeholderImage: UIImage(named: "place"))
            }

            addSubview(centerImg)
        }else{//多张图片需要滚动
            //scrollview
            scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
            addSubview(scrollView!)
            scrollView?.delegate = self
            scrollView?.pagingEnabled = true
            scrollView?.contentSize = CGSize(width: frame.size.width*3, height: frame.size.height)
            scrollView?.showsHorizontalScrollIndicator = false

            //images
            leftImg.contentMode = .ScaleAspectFill
            centerImg.contentMode = .ScaleAspectFill
            rightImg.contentMode = .ScaleAspectFill
            leftImg.layer.masksToBounds = true
            centerImg.layer.masksToBounds = true
            rightImg.layer.masksToBounds = true

            scrollView!.addSubview(leftImg)
            scrollView!.addSubview(rightImg)
            scrollView!.addSubview(centerImg)

            leftImg.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)
            centerImg.frame = CGRect(x: frame.size.width, y: 0, width: frame.size.width, height: frame.size.height)
            rightImg.frame = CGRect(x: frame.size.width*2, y: 0, width: frame.size.width, height: frame.size.height)

            addTapGesWithImage(leftImg)
            addTapGesWithImage(centerImg)
            addTapGesWithImage(rightImg)

            //pagecontrol
            pageControl.center = CGPoint(x: frame.size.width/2, y: frame.size.height-15)
            addSubview(pageControl)
            pageControl.currentPage = 0
            pageControl.numberOfPages = urlArr.count
            switch type {
            case .Image:
                pageControl.numberOfPages = imageArr.count
            default:
                pageControl.numberOfPages = urlArr.count
            }
            pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
            pageControl.currentPageIndicatorTintColor = UIColor.whiteColor()

            //初始化数据
            index = 0
            setImages()

            //timer
            openTimer()
        }
    }
    //通过index确定leftIndex和rightIndex的值
    private func changeLeftIndeAndRightIndeWith(arr:[AnyObject]){
        if arr.count == 0 {
            return
        }
        leftIndex = index - 1
        if leftIndex<0 {
            leftIndex = (arr.count)-1
        }
        rightIndex = index + 1
        if rightIndex>(arr.count)-1 {
            rightIndex = 0
        }
        centerImg.tag = index
        leftImg.tag = leftIndex
        rightImg.tag = rightIndex
    }
    //给图片添加点击手势
    private func addTapGesWithImage(image:UIImageView) {
        let tap = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
        image.userInteractionEnabled = true
        image.contentMode = .ScaleAspectFill
        image.clipsToBounds = true
        image.addGestureRecognizer(tap)
    }

    //将centerImg移动到显示位置 改变三个imageView显示的图片
    private func setImages(){
        scrollView?.setContentOffset(CGPoint(x: frame.size.width,y:0), animated: false)
        pageControl.currentPage = index

        switch type {
        case .Image:
            leftImg.image = imageArr[leftIndex]
            centerImg.image = imageArr[index]
            rightImg.image = imageArr[rightIndex]
        default:
            let leftUrl = urlArr[leftIndex]
            let rightUrl = urlArr[rightIndex]
            let centerUrl = urlArr[index]

            leftImg.setMyImageWithURL(NSURL(string: leftUrl), placeholderImage: UIImage(named: "place"))
            rightImg.setMyImageWithURL(NSURL(string: rightUrl), placeholderImage: UIImage(named: "place"))
            centerImg.setMyImageWithURL(NSURL(string:centerUrl), placeholderImage: UIImage(named: "place"))
        }
    }

    //点击图片,调用block
    @objc private func tap(ges:UITapGestureRecognizer) {
        clickBlock((ges.view?.tag)!)
    }
    //自动滚动
    @objc private func startScroll() {
        scrollView?.setContentOffset(CGPoint(x: frame.size.width*2,y:0), animated: true)
    }
    //scrollview代理,用来判断滚动方向
    func scrollViewDidScroll(scrollView: UIScrollView) {
        switch type {
        case .Image:
            changeIndexWith(imageArr)
        default:
            changeIndexWith(urlArr)
        }

    }
    //通过scrollview偏移量确定index的值
    private func changeIndexWith(arr:[AnyObject]) {
        if arr.count == 0 {
            return
        }
        if scrollView!.contentOffset.x<=0 {
            index -= 1
            if index<0 {
                index = arr.count-1;
            }
            setImages()
        }else if scrollView!.contentOffset.x>=frame.size.width*2{
            index += 1;
            if index>arr.count-1 {
                index = 0;
            }
            setImages()
        }
    }

    //scrollview代理,用于在手指拖动scrollview时关闭定时器
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        closeTimer()
    }
    private func closeTimer(){
        timer?.invalidate()
        timer = nil
    }
    //scrollview代理,手指离开时开启一个新的定时器
    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        openTimer()
    }
    private func openTimer(){
        timer = NSTimer.scheduledTimerWithTimeInterval(interval, target: self, selector: #selector(startScroll), userInfo: nil, repeats: true)
    }
}

可使用xib恐怕storyboard创造,使用办法:

@IBOutlet weak var URLsSlide: SlideImages!      //url
@IBOutlet weak var imagesSlide: SlideImages!    //本地图片

        URLsSlide.interval = 1
        URLsSlide.setURLsWithArr(["http://www.szgushang.comslider/bg1.jpg","http://www.szgushang.comslider/bg2.jpg","http://www.szgushang.comslider/bg3.jpg"]) { (index) in
            print("点击了第\(index)张图片")
        }

        imagesSlide.interval = 1.3
        imagesSlide.setImagesWithArr([UIImage(named: "image1.jpg")!,UIImage(named: "image2.jpg")!,UIImage(named: "image3.jpg")!,UIImage(named: "image4.jpg")!]) { (index) in
            print("点击了第\(index)张图片")
        }

  如图,整个结构正是左右箭头、底部小点以及内同叁大学一年级部分。即便是大概版本,不过我要么加了多少个小作用:是还是不是自动起先轮播、轮播间隔、内容是还是不是居中对齐、代码控制当前页码。整个逻辑也就200多行代码,小编就不多做讲解了,感兴趣的能够去拉取开源代码:

二、onFinishInflate()中获得大家需求的控件

@Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View view = LayoutInflater.from(context).inflate(R.layout.carousel_layout,null);
        this.viewPager = (ViewPager) view.findViewById(R.id.gallery);
        this.carouselLayout = (LinearLayout)view.findViewById(R.id.CarouselLayoutPage);
        pageItemWidth = ConvertUtils.dip2px(context,5);
        this.viewPager.addOnPageChangeListener(this);
        addView(view);
    }

onFinishInflate()方法是自定义控件中常用的三个方法,它代表从XML加载组件完毕了,在该方式中大家由此LayoutInflater.from(context).inflate
获取到个ViewPager对象和carouselLayout对象,并对pageItemWidth举行了赋值。 
与此同时为viewPager设置addOnPageChangeListener。那里别忘记调用addView();不然控件就显得不了啦!

2、onFinishInflate()中赢得大家供给的控件

@Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View view = LayoutInflater.from(context).inflate(R.layout.carousel_layout,null);
        this.viewPager = (ViewPager) view.findViewById(R.id.gallery);
        this.carouselLayout = (LinearLayout)view.findViewById(R.id.CarouselLayoutPage);
        pageItemWidth = ConvertUtils.dip2px(context,5);
        this.viewPager.addOnPageChangeListener(this);
        addView(view);
    }

onFinishInflate()方法是自定义控件中常用的八个措施,它象征从XML加载组件实现了,在该措施中大家透过LayoutInflater.from(context).inflate
获取到个ViewPager对象和carouselLayout对象,并对pageItemWidth进行了赋值。
并且为viewPager设置addOnPageChangeListener。那里别忘记调用addView();不然控件就显得不了啦!

Demo地址

三、通过设置set方法来获取数据,同时起头化界面效果

到这一步我们曾经得到到了突显轮播图的ViewPager对象,那接下去要让它显得你早晚想到了写个类继承PagerAdapter,然后重写getCount,isViewFromObject,isViewFromObject,destroyItem等办法来让ViewPager展示轮播图。可是大家又无法写得太稳定,因为或然种种人想要呈现的数码不壹致,所以大家定义三个接口来给外部使用的人写自身的逻辑。上代码:

//定义一个接口让外部设置展示的View
public interface Adapter{
        boolean isEmpty();
        View getView(int position);
        int getCount();
    }

//ViewPager的适配器
class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return totalCount;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object isViewFromObject(ViewGroup container, int position) {
            position %= showCount;
            //调用接口的getView()获取使用者要展示的View;
            View view = adapter.getView(position);
            container.addView(view);
            return view;
        }

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

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public void finishUpdate(ViewGroup container) {
            super.finishUpdate(container);

            int position = viewPager.getCurrentItem();
            //实现Viewpager到第一页的实现能向左滑动
            if (position==0){
                position=showCount;
                viewPager.setCurrentItem(position,false);

            }else if (position==totalCount-1){//ViewPager到最后一页的实现向又滑动
                position = showCount - 1;
                viewPager.setCurrentItem(position,false);
            }
        }
    }

//为外部提供设置数据源的方法,同时为ViewPager做展示
public void setAdapter(Adapter adapter){
        this.adapter = adapter;
        if (adapter!=null){
            init();
        }
    }

地点的代码正是概念了一个接口让外部来安装数据,提供setAdapter来为adapter赋值,同时起初化界面效果,init()方法中正是数额的先导化,代码如下:

private void init() {
        viewPager.setAdapter(null);
        carouselLayout.removeAllViews();//清空之前的数据
        if (adapter.isEmpty()){
            return;
        }
        int count = adapter.getCount();
        showCount = adapter.getCount();
        for (int i=0;i<count;i++){
            View view = new View(context);
            //用来做指示器的View,通过state来做展示效果
            if (currentPosition==i){
                view.setPressed(true);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(pageItemWidth + ConvertUtils.dip2px(context,3),pageItemWidth + ConvertUtils.dip2px(context,3));
                params.setMargins(pageItemWidth, 0, 0, 0);
                view.setLayoutParams(params);
            }else {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(pageItemWidth,pageItemWidth);
                params.setMargins(pageItemWidth,0,0,0);
                view.setLayoutParams(params);
            }
            view.setBackgroundResource(R.drawable.carousel_layout_page);
            carouselLayout.addView(view);
        }
        viewPager.setAdapter(new ViewPagerAdapter());
        viewPager.setCurrentItem(0);

        //让手指触碰到的时候自动轮播不起效
        this.viewPager.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        isUserTouched = true;
                        break;
                    case MotionEvent.ACTION_UP:
                        isUserTouched = false;
                        break;
                }
                return false;
            }
        });
        mTimer.schedule(mTimerTask, 3000, 3000);
    }

根本的逻辑代码正是这样呀,1个轮播图的控件就做好了。下边来看一下应用:

三、通过安装set方法来获取数据,同时初阶化界面效果

到这一步我们早已取得到了显示轮播图的ViewPager对象,那接下去要让它展现你势必想到了写个类继承PagerAdapter,然后重写getCount,isViewFromObject,isViewFromObject,destroyItem等方法来让ViewPager体现轮播图。然则大家又不能够写得太稳定,因为或然每一种人想要显示的数额不同,所以我们定义二个接口来给外部使用的人写自身的逻辑。上代码:

//定义一个接口让外部设置展示的View
public interface Adapter{
        boolean isEmpty();
        View getView(int position);
        int getCount();
    }

//ViewPager的适配器
class ViewPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return totalCount;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object isViewFromObject(ViewGroup container, int position) {
            position %= showCount;
            //调用接口的getView()获取使用者要展示的View;
            View view = adapter.getView(position);
            container.addView(view);
            return view;
        }

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

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public void finishUpdate(ViewGroup container) {
            super.finishUpdate(container);

            int position = viewPager.getCurrentItem();
            //实现Viewpager到第一页的实现能向左滑动
            if (position==0){
                position=showCount;
                viewPager.setCurrentItem(position,false);

            }else if (position==totalCount-1){//ViewPager到最后一页的实现向又滑动
                position = showCount - 1;
                viewPager.setCurrentItem(position,false);
            }
        }
    }

//为外部提供设置数据源的方法,同时为ViewPager做展示
public void setAdapter(Adapter adapter){
        this.adapter = adapter;
        if (adapter!=null){
            init();
        }
    }

地点的代码正是概念了1个接口让外部来安装数据,提供setAdapter来为adapter赋值,同时初叶化界面效果,init()方法中正是数码的发轫化,代码如下:

private void init() {
        viewPager.setAdapter(null);
        carouselLayout.removeAllViews();//清空之前的数据
        if (adapter.isEmpty()){
            return;
        }
        int count = adapter.getCount();
        showCount = adapter.getCount();
        for (int i=0;i<count;i++){
            View view = new View(context);
            //用来做指示器的View,通过state来做展示效果
            if (currentPosition==i){
                view.setPressed(true);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(pageItemWidth + ConvertUtils.dip2px(context,3),pageItemWidth + ConvertUtils.dip2px(context,3));
                params.setMargins(pageItemWidth, 0, 0, 0);
                view.setLayoutParams(params);
            }else {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(pageItemWidth,pageItemWidth);
                params.setMargins(pageItemWidth,0,0,0);
                view.setLayoutParams(params);
            }
            view.setBackgroundResource(R.drawable.carousel_layout_page);
            carouselLayout.addView(view);
        }
        viewPager.setAdapter(new ViewPagerAdapter());
        viewPager.setCurrentItem(0);

        //让手指触碰到的时候自动轮播不起效
        this.viewPager.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        isUserTouched = true;
                        break;
                    case MotionEvent.ACTION_UP:
                        isUserTouched = false;
                        break;
                }
                return false;
            }
        });
        mTimer.schedule(mTimerTask, 3000, 3000);
    }

美高梅开户网址 ,关键的逻辑代码便是这么啦,三个轮播图的控件就搞好了。上面来看一下利用:

4、使用

xml中写咱俩的轮播图控件:

 <com.yangqiangyu.test.carouselview.CarouselView
        android:layout_width="match_parent"
        android:layout_height="220dp">

 </com.yangqiangyu.test.carouselview.CarouselView>

java代码中获得控件,同时设置接口

 CarouselView carouselView = (CarouselView) findViewById(R.id.CarouselView);
        carouselView.setAdapter(new CarouselView.Adapter() {
            @Override
            public boolean isEmpty() {
                return false;
            }

            @Override
            public View getView(int position) {
                View view = mInflater.inflate(R.layout.item,null);
                ImageView imageView = (ImageView) view.findViewById(R.id.image);
                imageView.setImageResource(mImagesSrc[position]);
                return view;
            }

            @Override
            public int getCount() {
                return mImagesSrc.length;
            }
        });

回到是不是为空,在getView(int
position)中return大家想回到的View,正是那般简单了啊。

对你有帮衬的话,记得给赞给评论啊!

源码下载请戳--》图形轮播

4、使用

xml中写咱俩的轮播图控件:

 <com.yangqiangyu.test.carouselview.CarouselView
        android:layout_width="match_parent"
        android:layout_height="220dp">

 </com.yangqiangyu.test.carouselview.CarouselView>

java代码中获得控件,同时安装接口

 CarouselView carouselView = (CarouselView) findViewById(R.id.CarouselView);
        carouselView.setAdapter(new CarouselView.Adapter() {
            @Override
            public boolean isEmpty() {
                return false;
            }

            @Override
            public View getView(int position) {
                View view = mInflater.inflate(R.layout.item,null);
                ImageView imageView = (ImageView) view.findViewById(R.id.image);
                imageView.setImageResource(mImagesSrc[position]);
                return view;
            }

            @Override
            public int getCount() {
                return mImagesSrc.length;
            }
        });

回去是还是不是为空,在getView(int
position)中return大家想回来的View,就是如此不难了哇。

对你有赞助的话,记得给赞给评论啊!

源码下载请戳--》图表轮播

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图