Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataBinding support? #5

Open
desugar-64 opened this issue Jan 2, 2019 · 1 comment
Open

DataBinding support? #5

desugar-64 opened this issue Jan 2, 2019 · 1 comment

Comments

@desugar-64
Copy link

Have you any plans to support DataBinding layouts?

@rongi
Copy link
Owner

rongi commented Apr 28, 2019

What do you mean? I've tried it recently, it works with data binding.

val articlesAdapter = Klaster.withViewHolder<DataBindingViewHolder>()
  .itemCount { articles.size }
  .viewHolder { _: Int, parent: ViewGroup ->
    val binding: ViewDataBinding = DataBindingUtil.inflate(
      layoutInflater, R.layout.article_list_item, parent, false
    )
    return@viewHolder DataBindingViewHolder(binding)
  }
  .bind { position ->
    val article = articles[position]
    this.bind(article, artileListHandlers)
  }
  .build()

interface ArticleListHandlers {
  fun onArticleClick(article: Article) {}
}

class DataBindingViewHolder(private val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {
  fun bind(viewData: Any, handlers: Any) {
    binding.setVariable(BR.viewData, viewData)
    binding.setVariable(BR.handlers, handlers)
    binding.executePendingBindings()
  }
}

This data binding stuff

  .viewHolder { _: Int, parent: ViewGroup ->
    val binding: ViewDataBinding = DataBindingUtil.inflate(
      layoutInflater, R.layout.article_list_item, parent, false
    )
    return@viewHolder DataBindingViewHolder(binding)
  }

can be moved to an extension function if needed. Or may be you mean something else?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants