Last active
May 1, 2020 22:05
-
-
Save raxityo/697ca00055d1775da438bf6b17d1c5bd to your computer and use it in GitHub Desktop.
A helper superclass that accepts a layout with data binding enabled.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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