美文网首页自定义View
可以左右自动轮播的ScrollView

可以左右自动轮播的ScrollView

作者: 鈤汌冈板 | 来源:发表于2019-06-27 14:54 被阅读0次

公司要求做左右自动轮播的广告图控件,具体实现如下:

package com.zufang.view.ScrollView;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.anst.library.LibUtils.MHandler;
import com.anst.library.LibUtils.common.LibDensityUtils;
import com.anst.library.LibUtils.imageload.LibImage;

/**
 * Copyright (C), 2018-2018, 912租房网
 * Author: 
 * Date: 2018/8/21 0021 下午 3:01
 * Description: 水平自动滚动ScrollView
 */

public class AutoScrollView extends HorizontalScrollView {

    private LinearLayout mContentLl;
    private static final int MESSAGE_SCROLL = 10010;
    private static int offset = 30; //滚动的时间间隔
    private int mPageSize, mItemWidth, mMaxScrollWidth;
    private int scollWidth = 1; //每次只移动的像素点
    private CountHandler mHander;
    private List<String> mDataList;
    private boolean mRefresh = false;
    private boolean mIsScrollStart = false;

    private static class CountHandler extends MHandler<AutoScrollView> {

        public CountHandler(AutoScrollView target) {
            super(target);
        }

        @Override
        public void handle(final AutoScrollView target, Message msg) {
            switch (msg.what) {
                case MESSAGE_SCROLL:
                    target.mIsScrollStart = true;
                    int scrollX = target.getScrollX();
                    int delay = 0;

                    if (scrollX >= target.mMaxScrollWidth) {
                        target.scrollTo(0, 0);
                    } else {
                        target.smoothScrollBy(target.scollWidth, 0);
                        delay = offset;
                    }
                    postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            sendEmptyMessage(MESSAGE_SCROLL);
                        }
                    }, delay);
                    break;

            }
        }
    }

    public AutoScrollView(Context context) {
        this(context, null);
    }

    public AutoScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AutoScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mDataList = new ArrayList<>();
        mHander = new CountHandler(this);
        mContentLl = new LinearLayout(getContext());
        mContentLl.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        int margin = LibDensityUtils.dp2px(3);
        mContentLl.setLayoutParams(params);
        setPadding(margin, 0, margin, 0);
        addView(mContentLl);
    }

    public void setImageData(final List<String> data) {
        final int pageSize = mDataList.size();
        mDataList = data;
        mContentLl.removeAllViews();
        mHander.removeMessages(MESSAGE_SCROLL);
        postDelayed(new Runnable() {
            @Override
            public void run() {
                int containerWidth = LibDensityUtils.getScreenWidth();
                AutoScrollView.this.mPageSize = pageSize;
                AutoScrollView.this.mItemWidth = containerWidth / 3;
                AutoScrollView.this.mMaxScrollWidth = mDataList.size() * mItemWidth;
                mDataList.addAll(mDataList);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mItemWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
                for (int i = 0; i < mDataList.size(); i++) {
                    ImageView imageView = new ImageView(getContext());
                    String url = mDataList.get(i);
                    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                    LibImage.getInstance().load(getContext(), imageView, url);
                    mContentLl.addView(imageView, params);
                }
                mRefresh = true;
            }
        }, 100);
        start();
    }

    public boolean isStarted() {
        return mIsScrollStart;
    }

    private void start() {

        mHander.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mRefresh) {  // 防止多次刷新速度重叠加快
                    mHander.removeMessages(MESSAGE_SCROLL);
                    mRefresh = false;
                }
                mHander.sendEmptyMessage(MESSAGE_SCROLL);
            }
        }, offset);
    }
}

在页面中使用:

package com.zufang.view.homepage.rollad;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.anst.library.LibUtils.common.LibListUtils;
import com.anst.library.LibUtils.http.IHttpCallBack;
import com.anst.library.LibUtils.http.LibHttp;
import com.zufang.entity.response.HomeOccupyResponse;
import com.zufang.entity.response.PartnerItem;
import com.zufang.ui.R;
import com.anst.library.LibUtils.url.UrlConstant;
import com.zufang.ui.xuanzhi.XuanZhiFilterActivity;
import com.zufang.view.ScrollView.AutoScrollView;

/**
 * Copyright (C), 2018-2018, 912租房网
 * Author: 
 * Date: 2018/5/29 0029 下午 2:59
 * Description: 首页轮播广告
 */

public class RollAdView extends RelativeLayout {

    private TextView mPartenerTv;
    private AutoScrollView mAdScrollView; //合作品牌轮播图

    public RollAdView(Context context) {
        super(context);
        init();
    }

    public RollAdView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RollAdView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View view = inflate(getContext(), R.layout.view_roll_ad, this);
        mPartenerTv = (TextView) view.findViewById(R.id.tv_partnert_title);
        mAdScrollView = (AutoScrollView) view.findViewById(R.id.auto_scroll_view);
    }

    public void getData() {
        getAdData(); // 请求接口
    }


    //获取广告、轮播图
    private void getAdData() {
        LibHttp.getInstance().get(getContext(), UrlConstant.getInstance().HOMEPAGE_SPECIAL_PAGE, null, new IHttpCallBack<HomeOccupyResponse>() {
            @Override
            public void onSuccess(HomeOccupyResponse o) {
                if (o == null) {
                    return;
                }
                mPartenerTv.setText(o.partnerTitle);
                mPartenerTv.setVisibility(TextUtils.isEmpty(o.partnerTitle) ? GONE : VISIBLE);
                if (!LibListUtils.isListNullorEmpty(o.partnerList)) {
                    List<String> adList = new ArrayList<>();
                    for (PartnerItem item : o.partnerList) {
                        if (item == null) {
                            continue;
                        }
                        adList.add(item.imgUrl);
                    }
                    //为轮播控件赋值
                    mAdScrollView.setImageData(adList);
                }
            }

            @Override
            public void onFailed(String error) {
            }
        });
    }
}

相关文章

  • 可以左右自动轮播的ScrollView

    公司要求做左右自动轮播的广告图控件,具体实现如下: 在页面中使用:

  • js轮播图

    最近在网上找了很多写轮播图的例子,有些只有左右轮播,没有自动轮播,而有些只有自动轮播,没有左右轮播,有些又没有下方...

  • android轮播图效果

    先上效果图: viewpager+handler+runnableTask实现轮播图效果。可以自动轮播,左右无限滑...

  • 传统&呼吸 轮播

    传统的轮播图 一个 carousel 轮播图,图片实现自动轮播,可以左右按钮播放,点小圆点也能实现换图。同时设置节...

  • 简单广告轮播demo

    思路: scrollview pageControl开启,利用三张图片实现轮播 ,始终显示中间view,通过左右滑...

  • scrollView自动布局,自适应contentSize (x

    在使用scrollView进行自动布局时发现,scrollView的自动布局是相对来说比较复杂的,在设置完上下左右...

  • Block回调制作成的ScrollView轮播图

    更新0.1 可以刷新scrollView控件了 用Block回调制作成的ScrollView轮播图,能很好简便的去...

  • ScrollView实现自动轮播

    import "ViewController.h" @interface ViewController () {N...

  • Android自定义带动画无限自动轮播的Banner控件

    显示的效果请往下看,先说一下需求,可以自动轮播,按下停止轮播,松手开始轮播,不可见时停止轮播,自动轮播时带动画,手...

  • JQ实现左右轮播效果

    这篇文章主要是实现图片左右轮播效果,功能:进入页面自动播放图片,鼠标悬浮,暂停图片轮播,鼠标离开,继续图片轮播,点...

网友评论

    本文标题:可以左右自动轮播的ScrollView

    本文链接:https://www.haomeiwen.com/subject/rbkrcctx.html