两种方式的数据绑定在Android中不起作用

时间:2019-01-08 22:43:23

标签: android data-binding kotlin android-databinding

我的自定义视图中出现以下错误

e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Could not find event 'isCheckedAttrChanged' on View type 'com.playground.components.SquareCheckBox' file:/Volumes/Data/Work/Amira_Playground/Playground/app/src/main/res/layout/recycler_view_basic_information_settings_item.xml loc:47:8 - 60:50 ****\ data binding error ****

这是我的课程

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

    private var layout: ConstraintLayout
    private var checkImageView: ImageView
    private var iconImageView: ImageView
    private var titleTextView: TextView

    var isChecked: Boolean = (false)
    var iconImage: Int = -1
    var title: String = ""

    init {

        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val view = inflater.inflate(R.layout.item_square_checkbox, this, true)

        layout = view.layout
        checkImageView = view.checkImageView
        iconImageView = view.iconImageView
        titleTextView = view.titleTextView

        initAttributes(attrs)
        applyUIChanges()
    }

    private fun initAttributes(attrs: AttributeSet?) {

        attrs?.let {
            val typedArray = context.obtainStyledAttributes(it, R.styleable.square_checkbox_attributes, 0, 0)
            isChecked = typedArray.getBoolean(R.styleable.square_checkbox_attributes_isChecked, false)
            iconImage = typedArray.getResourceId(R.styleable.square_checkbox_attributes_iconImage, -1)
            title = typedArray.getString(R.styleable.square_checkbox_attributes_title)

            typedArray.recycle()
        }
    }

    fun applyUIChanges() {

        if (isChecked) {
            checkImageView.visibility = View.VISIBLE
            titleTextView.setTextColor(resources.getColor(android.R.color.black))
            layout.setBackgroundResource(R.drawable.xml_square_checkbox_selected)
        } else {
            checkImageView.visibility = View.INVISIBLE
            titleTextView.setTextColor(resources.getColor(R.color.lightGray))
            layout.setBackgroundResource(R.drawable.xml_square_checkbox_unselected)
        }

        if (iconImage != -1) {
            iconImageView.setImageResource(iconImage)
        }

        titleTextView.setText(title)
    }

    fun performOnClick() {
        isChecked = !isChecked
        applyUIChanges()
    }

    companion object {

        @JvmStatic
        @BindingAdapter("app:isChecked")
        fun setIsChecked(view: SquareCheckBox, checked: Boolean) {

            view.isChecked = checked
            view.applyUIChanges()
        }


        @JvmStatic
        @InverseBindingAdapter(attribute = "app:isChecked")
        fun getIsChecked(view: SquareCheckBox): Boolean {
            return view.isChecked
        }


        @JvmStatic
        @BindingAdapter(value = ["app:isCheckedChanged"])
        fun setListener (view: SquareCheckBox , listener: InverseBindingListener) {

            if (listener != null) {


            }
        }
    }
}

0 个答案:

没有答案