HomeTestView.kt 3.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package com.yidianling.home.ui.view

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.yidianling.home.R
import com.yidianling.home.event.HomeImpl
import com.yidianling.home.event.IHomeEvent
import com.yidianling.home.model.bean.HomeTestItemBean
konghaorui committed
11
import kotlinx.android.synthetic.xlzx.home_test_view.view.*
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述: 心灵*电台模块
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/02/13
 */
class HomeTestView(private val mContext: Context, private var homeEvent: HomeImpl?) : LinearLayout(mContext) {

    private var cacheList : ArrayList<HomeTestItemBean> = ArrayList()

    init {
        initView()
    }

    private fun initView() {
        orientation = VERTICAL
        val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        layoutParams = params
konghaorui committed
32
        View.inflate(mContext, R.layout.home_test_view, this)
33 34 35 36 37 38 39 40 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
        homeModuleIntelligentViewHomeCommonTitleView.setTitle("测试·分析")
        homeModuleIntelligentViewHomeCommonTitleView.setOnClickListener {
            homeEvent?.testMoreClick()
        }
    }

    fun initData(list: List<HomeTestItemBean>?) {
        //添加View
        if(list==null){
            return
        }
        if (cacheList.size != list?.size){
            cacheList.clear()
            cacheList.addAll(list!!)
            updateHomeTestItemViewNumber()
        }

        for (index in 0 until homeModuleIntelligentViewAddLayout.childCount){
            val itemView = homeModuleIntelligentViewAddLayout.getChildAt(index)
            if (itemView is HomeTestItemView){
                //第一个这种类型item,需要设置marginTop = 16dp,最后一个item需要设置marginBototm = 16dp
                if (index == 1) {
                    itemView.setTopMargin()
                }
//                else if (index == homeModuleIntelligentViewAddLayout.childCount -1) {
//                    itemView.setBottomMargin()
//                }
                itemView.updateData(list[index],index == list.size - 1)
            }else if (itemView is HomeTestTopItemView) {
                itemView.updateData(list[index])
            }
        }
    }

    /**
     * 更新itemView的数量以适应新的数据集
     */
    private fun updateHomeTestItemViewNumber() {
        var childCountRecord = homeModuleIntelligentViewAddLayout.childCount
        while (cacheList.size > childCountRecord) {
            if (childCountRecord == 0) {
                homeModuleIntelligentViewAddLayout.addView(HomeTestTopItemView(mContext, homeEvent))
            }else {
                homeModuleIntelligentViewAddLayout.addView(HomeTestItemView(mContext, homeEvent))
            }
            childCountRecord++
        }
        while (cacheList.size < childCountRecord &&
                (homeModuleIntelligentViewAddLayout.getChildAt(childCountRecord -1 ) is HomeTestItemView
                        || homeModuleIntelligentViewAddLayout.getChildAt(childCountRecord -1 ) is HomeTestTopItemView)) {
            homeModuleIntelligentViewAddLayout.removeViewAt(childCountRecord -1 )
            childCountRecord--
        }
    }
}