You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义View修改内容时出现重复渲染问题求助

自定义View重复渲染异常排查与解决

在实现自定义KitChoiceItemView并调用setupToHome()、setupToPlace()方法修改文本和控件可见性时,出现了View重复渲染的异常,表现为同一View的内容重复显示。


问题代码

自定义View实现

class KitChoiceItemView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0): ConstraintLayout(context, attrs, defStyle) {

private var binding: KitChooiceItemViewBinding

private var isActive = false

init {
    LayoutInflater.from(context).inflate(R.layout.kit_chooice_item_view, this, true)
    binding = KitChooiceItemViewBinding.inflate(LayoutInflater.from(context), this, true)
}

fun setupToHome() {
    binding.iconCircle.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_insc_home))
    binding.titleText.text = "Entrega a domicilio"
    binding.descriptionText.text = "Agrega una dirección para que podamos hacerte entrega de tu Kit a esa ubicación"
    binding.priceText.visibility = View.VISIBLE
    binding.detailsButton.visibility = View.GONE

    binding.selectButton.setOnClickListener {
        if (isActive) {
            isActive = false
            binding.cardView.background = ContextCompat.getDrawable(context, R.drawable.gray_border)
        } else {
            isActive = true
            binding.cardView.background = ContextCompat.getDrawable(context, R.drawable.kit_item_selected)
        }
    }
}

fun setupToPlace() {
    binding.iconCircle.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_insc_location))
    binding.titleText.text = "Recoger kit en la expo"
    binding.descriptionText.text = "Al seleccionar esta opción, omite el paso de entrega a domicilio y continuas para realizar el pago."
    binding.priceText.visibility = View.GONE
    binding.detailsButton.visibility = View.VISIBLE

    binding.selectButton.setOnClickListener {
        if (isActive) {
            isActive = false
            binding.cardView.background = ContextCompat.getDrawable(context, R.drawable.gray_border)
        } else {
            isActive = true
            binding.cardView.background = ContextCompat.getDrawable(context, R.drawable.kit_item_selected)
        }
    }

}}

布局文件中的引用

<com.asdeporte.asdeportev2.ui.reusableview.inscription.KitChoiceItemView
            android:id="@+id/address_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"/>
<com.asdeporte.asdeportev2.ui.reusableview.inscription.KitChoiceItemView
            android:id="@+id/delivery_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"/>

状态设置代码

binding.addressView.setupToHome()
binding.deliveryView.setupToPlace()

问题原因与修复

核心问题

重复渲染的根源在于init块中两次执行了布局添加操作:

  1. 第一行LayoutInflater.from(context).inflate(R.layout.kit_chooice_item_view, this, true)已经将布局文件填充并添加到当前ConstraintLayout实例中;
  2. 第二行KitChooiceItemViewBinding.inflate(LayoutInflater.from(context), this, true)再次执行了填充并添加布局的操作,导致同一布局被添加了两次,最终出现重复渲染的视觉效果。

修复后的代码

将init块修改为仅通过ViewBinding完成布局填充与绑定,移除多余的inflate调用:

init {
    binding = KitChooiceItemViewBinding.inflate(LayoutInflater.from(context), this, true)
}

额外优化点

setupToHome()和setupToPlace()中重复设置了相同的点击事件逻辑,可将其提取到init块中,避免多次设置导致的冗余:

init {
    binding = KitChooiceItemViewBinding.inflate(LayoutInflater.from(context), this, true)
    // 统一设置点击事件
    binding.selectButton.setOnClickListener {
        isActive = !isActive
        val backgroundRes = if (isActive) R.drawable.kit_item_selected else R.drawable.gray_border
        binding.cardView.background = ContextCompat.getDrawable(context, backgroundRes)
    }
}

// 修改后的setup方法移除重复的点击事件代码
fun setupToHome() {
    binding.iconCircle.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_insc_home))
    binding.titleText.text = "Entrega a domicilio"
    binding.descriptionText.text = "Agrega una dirección para que podamos hacerte entrega de tu Kit a esa ubicación"
    binding.priceText.visibility = View.VISIBLE
    binding.detailsButton.visibility = View.GONE
}

fun setupToPlace() {
    binding.iconCircle.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_insc_location))
    binding.titleText.text = "Recoger kit en la expo"
    binding.descriptionText.text = "Al seleccionar esta opción, omite el paso de entrega a domicilio y continuas para realizar el pago."
    binding.priceText.visibility = View.GONE
    binding.detailsButton.visibility = View.VISIBLE
}

内容的提问来源于stack exchange,提问作者GusDev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 07:25:40