HomePagerBannerView.kt 4.92 KB
Newer Older
徐健 committed
1 2 3 4 5 6 7
package com.yidianling.home.ui.view

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.RelativeLayout
徐健 committed
8
import com.ydl.ydlcommon.view.banner.GlideImageLoader
9 10
import com.yidianling.common.tools.RxDeviceTool
import com.yidianling.common.tools.RxImageTool
徐健 committed
11
import com.yidianling.home.R
12
import com.yidianling.home.event.IHomeBaseEvent
徐健 committed
13
import com.yidianling.home.model.bean.HomeHeaderBean
konghaorui committed
14
import kotlinx.android.synthetic.ydl.home_pager_banner_view.view.*
徐健 committed
15 16 17 18 19 20 21 22 23


/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述:顶部ViewPager与分类模块
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/02/13
 */
24
class HomePagerBannerView(private val mContext: Context, private var homeEvent: IHomeBaseEvent?) : RelativeLayout(mContext) {
徐健 committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

    private val mImageViews = ArrayList<String>()
    private var homePagerBannerCategoryView: HomePagerBannerCategoryItemView? = null
    //实时测评数据View
    private var realTestView: HomePagerBannerRealView? = null
    private var pageContainer: HomeCategoryContainer? = null

    init {
        initView()
    }

    private fun initView() {
        var screenWidth = RxDeviceTool.getScreenWidth(mContext)
        val params = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        layoutParams = params
konghaorui committed
40
        View.inflate(mContext, R.layout.home_pager_banner_view, this)
徐健 committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

        initBannerLayout()
//        val addLayoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//        addLayoutParams.setMargins(0, (ScreenUtil.screenWidth * (356f / 750f)).toInt(),0,0)
//        homeModulePagerBannerViewAddLayout.layoutParams = addLayoutParams
        initPageContainer()
        initRealTextView()
    }

    private fun initPageContainer() {
        if (null == pageContainer) {
            var containerParam = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            pageContainer = HomeCategoryContainer(context, homeEvent!!)
            pageContainer!!.layoutParams = containerParam
            homeModulePagerBannerViewAddLayout.addView(pageContainer)
        }
    }

    private fun initBannerLayout() {
        val screenWidth = RxDeviceTool.getScreenWidth(mContext)
        val viewHeight = screenWidth * 508 / 750
        val params = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, viewHeight)
        homeModulePagerBannerViewBanner.layoutParams = params
        homeModulePagerBannerViewBanner.setIndicatorBottomPadding(68)

        initHomeModulePagerBannerViewAddLayout(viewHeight)
    }

    private fun initHomeModulePagerBannerViewAddLayout(viewHeight : Int){
        val params = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
        params.setMargins(0,(viewHeight * 0.76).toInt(),0,0)
        homeModulePagerBannerViewAddLayout.layoutParams = params
        homeModulePagerBannerViewAddLayout.setPadding(0,RxImageTool.dip2px(24f),0,0)
    }

    /**
     * 填充数据
     */
    fun initData(bean: HomeHeaderBean?) {
        initBanner(bean?.focusList)
        pageContainer?.initData(bean)
        //设置实时预约数据
        realTestView!!.initData(bean?.homeSaleData)
    }

    /**
     * 初始化实时测评状态View
     */
    private fun initRealTextView() {
        if (null == realTestView) {
            var realTestParam = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            realTestView = HomePagerBannerRealView(context, homeEvent!!)
            realTestView!!.layoutParams = realTestParam
            homeModulePagerBannerViewAddLayout.addView(realTestView)
        }
    }

    /**
     * 更新banner
     */
    private fun initBanner(focusListBean: List<HomeHeaderBean.FocusListBean>?) {
        //临时缓存集合
        val cacheList = ArrayList<HomeHeaderBean.FocusListBean>()
        if (null != focusListBean && focusListBean.isNotEmpty()) {
            mImageViews.clear()
            //产品说取前8个
霍志良 committed
107
            cacheList.addAll(focusListBean)
徐健 committed
108 109 110 111
            for (item in cacheList) {
                mImageViews.add(item.imageUrl!!)
            }
        }
konghaorui committed
112
        homeModulePagerBannerViewBanner.setImageLoader(GlideImageLoader(R.drawable.home_image_default_back)).setImages(mImageViews).start()
徐健 committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

        homeModulePagerBannerViewBanner.setOnBannerListener {
            if (it < cacheList?.size) {
                homeEvent?.bannerClick(cacheList[it])
            }
        }
    }

    fun startBanner() {
        if (homeModulePagerBannerViewBanner != null) {
            homeModulePagerBannerViewBanner.startAutoPlay()
        }
    }

    fun stopBanner() {
        if (homeModulePagerBannerViewBanner != null) {
            homeModulePagerBannerViewBanner.stopAutoPlay()
        }
    }
}