威尼斯人线上娱乐

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

8 4月 , 2019  

1、要做成什么

背景

近年要做二个轮播图的功效,网上看了几篇小说,基本上都能找到完成,效果还挺不错,然而在写的时候觉得每一遍都要独立去重新在Activity里写一群代码。于是本人包装了弹指间。本篇轮播图达成原理原著出处:循环广告位组件的兑现,那里只是做了下封装成一个控件,不必每一回重复写代码了。

Android自定义控件之轮播图控件,轮播控件。背景

近些年要做四个轮播图的成效,网上看了几篇小说,基本上都能找到完毕,效果还挺不错,可是在写的时候觉得每一遍都要独立去重新在Activity里写一批代码。于是本人包装了弹指间。这里只是做了下封装成1个控件,不必每回重复写代码了。

尽管如此网上海大学把包装好的轮播图控件,可是作者认为依然有供给自个儿写一下。因为那样也总算壹种学习,而且自个儿写的事物更易于控制,用到祥和的项目里的时候更易于修改定制,也更易于精简项目代码。

  bs端的轮播控件千千万,有的还是可以够看做三个独门的库来开发,所提到到的效劳也是缤纷多彩。相对来说,cs端的轮播用得不多,笔者那边只是简短的做了个能满意壹般须要的轮播,在品种中凑会凑会依旧得以的。先给两张图,看看最后的效应:

效果图

威尼斯人线上娱乐 1

效果图

威尼斯人线上娱乐 2

自小编查了一部分资料,完成轮播图重要依靠ScrollView恐怕CollectionView来促成,总的来说使用CollectionView能够相比较简单的做1些特殊的轮转效应,可是不荒谬的话大家用到的轮播图都是用部分很基本的效果,个人觉得选拔ScrollView来做完全就能够了,那样控件的份额级会小部分。而且那里只封装基本作用,满意当先2伍%的须要,即便有须要再去定制,那样能够使项目里没用的代码越来越少壹些,那也是本身不用那多少个有无数酷炫效果,很多功效的外人封装的很好,也很好用的那多少个轮播控件的基本点原因。或然是精神分裂症,作者期待作者的类型里尽恐怕少的留存那么些并未有运行的代码,固然本身并不可能确定保障完全未有。

威尼斯人线上娱乐 3

兑现分析

轮播图的法力正是落实左右滑动的广告、图片新闻展现,那大家就用ViewPager来完结,由于挂念到用户体验,大家还要求在底下加三个提醒器来标示滑动到了第几张轮播图。提示器大家能够用3个线性布局来依据要显示的轮播图设置展现的View,大家要做这么的叁个控件未有何异样的职能,其实正是五个控件的咬合,只是我们要在里头处理好它们中间的相互关系(其实正是ViewPager滚动的时候,上面提示器的呈现),所以大家就用自定义控件个中的3结合方式来促成。 
下边开端

福寿双全分析

轮播图的职能正是兑现左右滑行的广告、图片音信展示,那大家就用ViewPager来落成,由于考虑到用户体验,我们还亟需在底下加二个提醒器来标示滑动到了第几张轮播图。提醒器大家得以用多少个线性布局来依据要展现的轮播图设置展现的View,大家要做如此的一个控件未有怎么独特的效应,其实正是七个控件的组合,只是我们要在里边处理好它们之间的竞相关系(其实就是ViewPager滚动的时候,下边提醒器的来得),所以大家就用自定义控件其中的结缘情势来兑现。
上面起首

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

威尼斯人线上娱乐 4

一、定义1个控件继承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里面了,上边正是要博得

一、定义3个控件继承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大片段。固然是简单版本,不过自个儿大概加了多少个小成效:是还是不是自动开首轮播、轮播间隔、内容是还是不是居中对齐、代码控制当前页码。整个逻辑也就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();不然控件就体现不了啦!

二、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地址

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

上面的代码正是概念了3个接口让外部来设置数据,提供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);
    }

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

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

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

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