轮播控件,Android自定义控件之轮播图控件

一、要做成什么

背景

近年要做3个轮播图的功能,网上看了几篇文章,基本上都能找到完成,效果还挺不错,不过在写的时候觉得每便都要单独去重新在Activity里写一批代码。于是本身包裹了眨眼间间。本篇轮播图完毕原理原来的小说出处:巡回广告位组件的落到实处,那里只是做了下封装成贰个控件,不必每回重复写代码了。

背景

近些年要做2个轮播图的功效,网上看了几篇小说,基本上都能找到完结,效果还挺不错,然则在写的时候觉得每一遍都要单独去重新在Activity里写一批代码。于是本身包装了一晃。那里只是做了下封装成三个控件,不必每一趟重复写代码了。

就算如此网上海南大学学把包装好的轮播图控件,可是自身认为还是有须要本身写一下。因为这么也究竟一种学习,而且自个儿写的事物更便于控制,用到本人的体系里的时候更易于修改定制,也更易于精简项目代码。

  bs端的轮播控件千千万,有的竟是能同日而语3个独自的库来开发,所关联到的功效也是缤纷多彩。相对来说,cs端的轮播用得不多,笔者那边只是简短的做了个能满意1般须要的轮播,在品种中凑会凑会仍然得以的。先给两张图,看看最终的职能:

效果图

亚洲必赢官网 1

效果图

亚洲必赢官网 2

自个儿查了一些材料,实现轮播图主要借助ScrollView或然CollectionView来完毕,总的来说使用CollectionView能够比较易于的做一些特种的轮转效应,可是正常的话大家用到的轮播图都以用1些很基本的作用,个人觉得使用ScrollView来做完全就可以了,那样控件的重量级会小片段。而且那里只封装基本作用,满意超越陆分之叁的要求,假诺有须求再去定制,那样能够使项目里没用的代码越来越少一些,那也是本人不用那个有好多酷炫效果,很多效用的人家封装的很好,也很好用的那一个轮播控件的首要性原因。或然是失眠,小编盼望作者的花色里尽可能少的存在这一个并未有运维的代码,即便笔者并无法有限支撑完全未有。

亚洲必赢官网 3

落到实处分析

轮播图的功力正是兑现左右滑动的广告、图片音信呈现,那我们就用ViewPager来达成,由于思考到用户体验,大家还必要在上面加1个提示器来标示滑动到了第几张轮播图。提醒器大家能够用1个线性布局来依据要展现的轮播图设置显示的View,大家要做那样的三个控件未有何独特的法力,其实便是四个控件的构成,只是大家要在里面处理好它们中间的竞相关系(其实正是ViewPager滚动的时候,上面提醒器的显得),所以大家就用自定义控件个中的咬合措施来贯彻。 
上面开首

贯彻分析

轮播控件,Android自定义控件之轮播图控件。轮播图的机能正是促成左右滑行的广告、图片音信呈现,那大家就用ViewPager来达成,由于思考到用户体验,大家还亟需在上边加二个提醒器来标示滑动到了第几张轮播图。提醒器大家得以用二个线性布局来遵照要出示的轮播图设置呈现的View,大家要做那样的二个控件未有怎么新鲜的功用,其实正是四个控件的重组,只是大家要在当中处理好它们之间的互相关系(其实正是ViewPager滚动的时候,上边提示器的呈现),所以大家就用自定义控件个中的构成措施来实现。
下边初阶

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

亚洲必赢官网 4

1、定义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,写一个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)张图片")
        }

  如图,整个结构就是反正箭头、尾巴部分小点以及内同3大学一年级部分。纵然是简约版本,可是笔者要么加了多少个小成效:是或不是自动开第3堆次播、轮播间隔、内容是否居中对齐、代码控制当前页码。整个逻辑也就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()方法是自定义控件中常用的3个措施,它意味着从XML加载组件完毕了,在该方法中大家通过LayoutInflater.from(context).inflate
获取到个ViewPager对象和carouselLayout对象,并对pageItemWidth实行了赋值。 
而且为viewPager设置addOnPageChangeListener。那里别忘记调用addView();不然控件就显得不了啦!

二、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();
        }
    }

下边包车型大巴代码便是概念了二个接口让外部来设置数据,提供set艾达pter来为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);
    }

驷不比舌的逻辑代码正是那样啦,五个轮播图的控件就做好了。下边来看一下采取:

3、通过安装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);
    }

重在的逻辑代码就是那样呀,贰个轮播图的控件就做好了。上面来看一下施用:

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地图