Skip to content

//workflow/com.squareup.workflow1.ui/LayoutRunner/Companion/bind

bind

[androidJvm]
Content
inline fun <BindingT : ViewBinding, RenderingT : Any> bind(noinline bindingInflater: ViewBindingInflater<BindingT>, crossinline showRendering: BindingT.(RenderingT, ViewEnvironment) -> Unit): ViewFactory<RenderingT>
More info

Creates a ViewFactory that inflates a ViewBinding (BindingT) to show renderings of type RenderingT, using a lambda.

1
2
3
4
5
val HelloBinding: ViewFactory<Rendering> =
  LayoutRunner.bind(HelloGoodbyeLayoutBinding::inflate) { rendering, viewEnvironment ->
    helloMessage.text = rendering.message
    helloMessage.setOnClickListener { rendering.onClick(Unit) }
  }

If you need to initialize your view before showRendering is called, implement LayoutRunner and create a binding using the bind variant that accepts a (ViewBinding) -> LayoutRunner function, below.

[androidJvm]
Content
inline fun <BindingT : ViewBinding, RenderingT : Any> bind(noinline bindingInflater: ViewBindingInflater<BindingT>, noinline constructor: (BindingT) -> LayoutRunner<RenderingT>): ViewFactory<RenderingT>
More info

Creates a ViewFactory that inflates a ViewBinding (BindingT) to show renderings of type RenderingT, using a LayoutRunner created by constructor. Handy if you need to perform some set up before showRendering is called.

class HelloLayoutRunner( private val binding: HelloGoodbyeLayoutBinding ) : LayoutRunner {

1
2
3
4
5
6
7
8
override fun showRendering(rendering: Rendering) {
  binding.messageView.text = rendering.message
  binding.messageView.setOnClickListener { rendering.onClick(Unit) }
}

companion object : ViewFactory<Rendering> by bind(
    HelloGoodbyeLayoutBinding::inflate, ::HelloLayoutRunner
)

}

If the view doesn’t need to be initialized before showRendering is called, use the variant above which just takes a lambda.

[androidJvm]
Content
inline fun <RenderingT : Any> bind(@LayoutRes()layoutId: Int, noinline constructor: (View) -> LayoutRunner<RenderingT>): ViewFactory<RenderingT>
More info

Creates a ViewFactory that inflates layoutId to show renderings of type RenderingT, using a LayoutRunner created by constructor. Avoids any use of AndroidX ViewBinding.