美文网首页自定义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

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