Skip to content

Instantly share code, notes, and snippets.

@raxityo
Last active May 1, 2020 22:05
Show Gist options
  • Save raxityo/697ca00055d1775da438bf6b17d1c5bd to your computer and use it in GitHub Desktop.
Save raxityo/697ca00055d1775da438bf6b17d1c5bd to your computer and use it in GitHub Desktop.
A helper superclass that accepts a layout with data binding enabled.
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
/**
* A helper superclass that accepts a layout with data binding enabled.
*
* Example usage:
* ```
* class FragmentAuth : DataBindingFragment<FragmentAuthBinding>(R.layout.fragment_auth)
* ```
* @param layout layout res id with data binding.
*/
open class DataBindingFragment<T : ViewDataBinding>(
@LayoutRes val layout: Int
) : Fragment() {
internal var binding: T? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
) = DataBindingUtil
.inflate<T>(inflater, layout, container, false)
.apply { binding = this }
.root
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment