Paul Woitaschek
09/27/2019, 10:55 AMwasyl
09/27/2019, 11:00 AMval binding = DataBindingUtil.inflate<TBinding>(inflater, layoutId, container, false)
binding.setVariable(BR.model, model)
Where layoutId
and model
are abstract properties on the base fragment. All layouts need to have a binding variable named model
and the types just need to matchwasyl
09/27/2019, 11:01 AMTBinding
generic parameter for this base fragment, ViewDataBinding
should work as wellPaul Woitaschek
09/27/2019, 11:10 AMwasyl
09/27/2019, 11:18 AMjw
09/27/2019, 12:16 PMTheBinding::inflate
to the supertype constructor whose type is parameterized on T : ViewBinding
and whose constructor accepts (LayoutInflater) -> T
Paul Woitaschek
09/27/2019, 12:44 PMjw
09/27/2019, 12:45 PMPaul Woitaschek
09/27/2019, 12:47 PMjw
09/27/2019, 12:51 PMPaul Woitaschek
09/27/2019, 12:53 PMjw
09/27/2019, 12:55 PMPaul Woitaschek
09/27/2019, 12:56 PMitnoles
12/29/2019, 4:20 AMopen class BaseFragment<T : ViewBinding>(val inflater: (LayoutInflater) -> T): Fragment() {
lateinit var binding: T
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater(inflater).root
}
This is what I didPaul Woitaschek
12/29/2019, 8:31 PMitnoles
12/29/2019, 11:57 PMitnoles
12/30/2019, 12:03 AMopen class BaseFragment<T : ViewBinding>(val inflater: (LayoutInflater, ViewGroup?, Boolean) -> T) :
Fragment()
{
lateinit var binding: T
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater(inflater, container, false).root
}
Paul Woitaschek
12/30/2019, 7:27 AMitnoles
12/30/2019, 5:36 PMopen class BaseFragment<T : ViewBinding>(private val inflater: (LayoutInflater, ViewGroup?, Boolean) -> T) :
Fragment()
{
lateinit var binding: T private set
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = inflater(inflater, container, false)
return binding.root
}
}
Done