kotlin view array

几百年不写自定义view了,全靠百度养活我

/**
 *
 *      ╭︿︿︿╮
 *    {/ .  . /}
 *     ( (oo) )
 *      ︶︶︶
 *    Create by cps 2025/7/14  15:03
 *
 */
class CleanStepInfoView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet, defStyleAttr: Int = 0
) :
    FrameLayout(context, attrs, defStyleAttr) {
    var bgResource: Int = 0


    val tvList = mutableListOf<String>()
    val ivList = mutableListOf<Drawable>()
    lateinit var bgIv: ImageView
    val ivWidgetList = mutableListOf<ImageView>()
    val tvWidgetList = mutableListOf<TextView>()

    init {
        context.obtainStyledAttributes(attrs, R.styleable.CleanStepInfoView).let {
            bgResource = it.getResourceId(
                R.styleable.CleanStepInfoView_step_bg_iv,
                R.drawable.manual_clean_info_bg
            )
            val ivArray = it.getResourceId(R.styleable.CleanStepInfoView_step_iv_array, 0)
            val tvArray = it.getResourceId(R.styleable.CleanStepInfoView_step_tv_array, 0)
            if (ivArray != 0) {
                context.resources.obtainTypedArray(ivArray).let { ivs ->
                    for (i in 0 until ivs.length()) {
                        ivs.getDrawable(i)?.let { drawable ->
                            ivList.add(drawable)
                        }
                    }
                    ivs.recycle()
                }
            }
            if (tvArray != 0) {
                context.resources.obtainTypedArray(tvArray).let { tvs ->
                    for (i in 0 until tvs.length()) {
                        tvs.getString(i)?.let { str ->
                            tvList.add(str)
                        }
                    }
                    tvs.recycle()
                }
            }
            it.recycle()
        }
        //-----------------------------------------------------------------------↓↓↓↓↓↓↓↓
        LayoutInflater.from(context).inflate(R.layout.clean_step_manual_tips, null).let {
            bgIv = it.findViewById(R.id.clean_step_info_bg_iv)
            tvWidgetList.add(it.findViewById<TextView>(R.id.clean_step_info_tv_0))
            tvWidgetList.add(it.findViewById<TextView>(R.id.clean_step_info_tv_1))
            tvWidgetList.add(it.findViewById<TextView>(R.id.clean_step_info_tv_2))
            tvWidgetList.add(it.findViewById<TextView>(R.id.clean_step_info_tv_3))
            ivWidgetList.add(it.findViewById(R.id.clean_step_info_iv_0))
            ivWidgetList.add(it.findViewById(R.id.clean_step_info_iv_1))
            ivWidgetList.add(it.findViewById(R.id.clean_step_info_iv_2))
            ivWidgetList.add(it.findViewById(R.id.clean_step_info_iv_3))
            resetViewConfigs()
            addView(
                it,
                LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
            )//使用 addview 就不能 inflate的时候 用root
        }
    }


    private fun resetViewConfigs() {
        bgIv.setImageResource(bgResource)
        tvWidgetList.forEachIndexed { index, textView ->
            if (index in tvList.indices) {
                textView.text = tvList[index]
            } else {
                textView.text = ""
            }
        }

        ivWidgetList.forEachIndexed { index, imageView ->
            if (index in ivList.indices) {
                imageView.setImageDrawable(ivList[index])
            } else {
                imageView.setImageDrawable(ColorDrawable(Color.TRANSPARENT))
            }

        }

    }


}
   <declare-styleable name="CleanStepInfoView">
        <attr name="step_bg_iv" format="reference"/>
        <attr name="step_iv_array" format="reference" />
        <attr name="step_tv_array" format="reference" />
    </declare-styleable>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容