溫馨提示×

Android中BindingAdapter的具體應(yīng)用有哪些

小樊
82
2024-08-10 20:54:38
欄目: 編程語言

  1. 設(shè)置ImageView的圖片加載:
@BindingAdapter("imageUrl")
fun loadImage(view: ImageView, url: String) {
    Glide.with(view.context).load(url).into(view)
}
  1. 設(shè)置TextView的文本顏色:
@BindingAdapter("textColor")
fun setTextColor(view: TextView, colorResId: Int) {
    view.setTextColor(ContextCompat.getColor(view.context, colorResId))
}
  1. 設(shè)置View的可見性:
@BindingAdapter("visibility")
fun setVisibility(view: View, isVisible: Boolean) {
    view.visibility = if (isVisible) View.VISIBLE else View.GONE
}
  1. 設(shè)置RecyclerView的數(shù)據(jù)列表:
@BindingAdapter("data")
fun setData(recyclerView: RecyclerView, data: List<Any>) {
    (recyclerView.adapter as MyAdapter).setData(data)
}
  1. 設(shè)置Button的點(diǎn)擊事件:
@BindingAdapter("clickListener")
fun setClickListener(button: Button, listener: View.OnClickListener) {
    button.setOnClickListener(listener)
}

0