I\'m trying to follow data-binding example from official google doc https://developer.android.com/tools/data-binding/guide.html
except that I\'m trying to apply data
Very helpful blog about Databinding : https://link.medium.com/HQY2VizKO1
class FragmentBinding(
@LayoutRes private val resId: Int
) : ReadOnlyProperty {
private var binding: T? = null
override operator fun getValue(
thisRef: Fragment,
property: KProperty<*>
): T = binding ?: createBinding(thisRef).also { binding = it }
private fun createBinding(
activity: Fragment
): T = DataBindingUtil.inflate(LayoutInflater.from(activity.context),resId,null,true)
}
Declare binding val like this in Fragment :
private val binding by FragmentBinding(R.layout.fragment_login)
Don't forget to write this in fragment
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return binding.root
}