1、要做成什么

背景

近来要做三个轮播图的效益,网上看了几篇文章,基本上都能找到落成,效果还挺不错,不过在写的时候感觉每一回都要独立去重新在Activity里写一群代码。于是本身包裹了一晃。本篇轮播图达成原理原来的作品出处:巡回广告位组件的达成,那里只是做了下封装成四个控件,不必每回重复写代码了。

背景

近日要做三个轮播图的效益,网上看了几篇小说,基本上都能找到达成,效果还挺不错,然则在写的时候感觉每趟都要独立去重新在Activity里写一群代码。于是自个儿包装了壹晃。那里只是做了下封装成一个控件,不必每趟重复写代码了。

就算网上海高校把包裹好的轮播图控件,不过我觉着如故有不可或缺本身写一下。因为那样也终于一种学习,而且本人写的东西更便于控制,用到祥和的项目里的时候更易于修改定制,也更便于精简项目代码。

  bs端的轮播控件千千万,有的甚至能看做一个独门的库来支付,所提到到的功能也是缤纷多彩。相对来说,cs端的轮播用得不多,作者那里只是不难的做了个能满意一般要求的轮播,在项目中凑会凑会还能的。先给两张图,看看最终的功力:

效果图

金沙注册送58 1

效果图

金沙注册送58 2

轮播控件,Android自定义控件之轮播图控件。本人查了1部分资料,完毕轮播图主要注重ScrollView或然CollectionView来促成,总的来说使用CollectionView可以相比便于的做1些新鲜的滚动作效果应,可是符合规律来说我们用到的轮播图都以用部分很基本的职能,个人认为使用ScrollView来做完全就足以了,那样控件的份额级会小部分。而且那里只封装基本成效,满足半数以上的急需,固然有亟待再去定制,那样能够使项目里没用的代码更加少一些,那也是自家不用这么些有广大酷炫效果,很多意义的外人封装的很好,也很好用的这一个轮播控件的严重性缘由。大概是网瘾,笔者希望自个儿的类型里尽大概少的存在这几个尚未运营的代码,固然作者并无法担保完全未有。

金沙注册送58 3

兑现分析

轮播图的效果正是落到实处左右滑行的广告、图片音讯展现,这大家就用ViewPager来达成,由于考虑到用户体验,咱们还索要在上面加一个提醒器来标示滑动到了第几张轮播图。提示器大家得以用二个线性布局来遵照要呈现的轮播图设置突显的View,大家要做这么的一个控件未有何新鲜的效能,其实就是多个控件的咬合,只是大家要在个中处理好它们之间的相互关系(其实正是ViewPager滚动的时候,下边提醒器的体现),所以我们就用自定义控件个中的结合措施来促成。 
上边初叶

福寿绵绵分析

轮播图的机能正是兑现左右滑动的广告、图片新闻展现,那我们就用ViewPager来达成,由于牵记到用户体验,大家还亟需在上面加三个提醒器来标示滑动到了第几张轮播图。提示器我们能够用2个线性布局来依据要彰显的轮播图设置展现的View,大家要做如此的2个控件未有怎么非凡的功能,其实正是四个控件的组合,只是大家要在中间处理好它们中间的交互关系(其实正是ViewPager滚动的时候,上边提醒器的显得),所以大家就用自定义控件在那之中的结缘措施来兑现。
上边开首

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

金沙注册送58 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,写二个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()方法是自定义控件中常用的2个艺术,它代表从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方法来获取数据,同时开端化界面效果

金沙注册送58,到这一步我们曾经收获到了突显轮播图的ViewPager对象,这接下去要让它展现你一定想到了写个类继承PagerAdapter,然后重写getCount,isViewFromObject,isViewFromObject,destroyItem等艺术来让ViewPager呈现轮播图。然则我们又不可能写得太稳定,因为可能每一个人想要展现的多少不平等,所以大家定义2个接口来给外部使用的人写自身的逻辑。上代码:

//定义一个接口让外部设置展示的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);
    }

主要的逻辑代码正是那般呀,叁个轮播图的控件就抓牢了。上边来看一下接纳:

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