HomeAssuageGriefView.kt 2.5 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.LinearLayout
import com.yidianling.home.R
8
import com.yidianling.home.event.IHomeBaseEvent
徐健 committed
9
import com.yidianling.home.model.bean.HomeAskBean
konghaorui committed
10
import kotlinx.android.synthetic.ydl.home_assuage_grief_view.view.*
徐健 committed
11 12 13 14 15 16 17 18

/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述: 解忧*问答模块
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/02/13
 */
19
class HomeAssuageGriefView(private val mContext: Context, private var homeEvent: IHomeBaseEvent?) : LinearLayout(mContext) {
徐健 committed
20 21 22 23 24 25 26 27 28 29 30

    private var cacheList: ArrayList<HomeAskBean.DataBean> = ArrayList()

    init {
        initView()
    }

    private fun initView() {
        orientation = VERTICAL
        val params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        layoutParams = params
konghaorui committed
31
        View.inflate(mContext, R.layout.home_assuage_grief_view, this)
徐健 committed
32 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
        homeModuleAssuageGriefViewHomeCommonTitleView.setTitle("解忧·问答")
        homeModuleAssuageGriefViewHomeCommonTitleView.setOnClickListener {
            homeEvent?.askMoreClick()
        }
    }

    fun initData(position: Int, list: List<HomeAskBean.DataBean>?) {
        //添加View
        list?.let {
            if (null != list && cacheList.size != list?.size) {
                cacheList.clear()
                cacheList.addAll(list!!)
                updateHomeAssuageGriefItemViewNumber()
            }
            //刷新数据
            for (index in 0..childCount) {
                val itemView = getChildAt(index)
                if (itemView is HomeAssuageGriefItemView) {
                    if ((index - 1) < list!!.size) {
                        itemView.updateData(list!![index - 1], position, index - 1)
                    }
                }
            }
        }
    }

    /**
     * 更新itemView的数量以适应新的数据集
     */
    private fun updateHomeAssuageGriefItemViewNumber() {
        var childCountRecord = childCount - 1 //减1是因为第一个子View是标题View
        while (cacheList.size > childCountRecord) {
            addView(HomeAssuageGriefItemView(mContext, homeEvent))
            childCountRecord++
        }
        while (cacheList.size < childCountRecord && getChildAt(childCountRecord) is HomeAssuageGriefItemView) {
            removeViewAt(childCountRecord)
            childCountRecord--
        }
    }
}