Last active
August 9, 2021 01:07
-
-
Save dancing-koala/55c9057c9c58930b6fc67cadc457d56f to your computer and use it in GitHub Desktop.
Reduce boilerplate for view binding in fragments
This file contains hidden or 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
| // Just for customizing the name |
This file contains hidden or 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
| // This alias makes the code more readable | |
| typealias BindingInflater<VB> = (inflater: LayoutInflater, container: ViewGroup?, attachToRoot: Boolean) -> VB |
This file contains hidden or 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
| abstract class BaseFragment<VB : ViewBinding> : Fragment() { | |
| // This property must be nullable to prevent view leaks | |
| // Never forget that fragments outlive their views ! | |
| private var mutableViewBinding: VB? = null | |
| // This property gives us a not nullable view binding for simpler use | |
| protected val viewBinding: VB get() = mutableViewBinding!! | |
| // This property is abstract because it has to be declared by the child class | |
| protected abstract val bindingInflater: BindingInflater<VB> | |
| override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | |
| mutableViewBinding = bindingInflater.invoke(inflater, container, false) | |
| return viewBinding.root | |
| } | |
| override fun onDestroyView() { | |
| super.onDestroyView() | |
| // Prevent view leaks | |
| mutableViewBinding = null | |
| } | |
| } |
This file contains hidden or 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
| android { | |
| ... | |
| // Enable view binding for module | |
| buildFeatures { | |
| viewBinding true | |
| } | |
| ... | |
| } |
This file contains hidden or 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
| class ExampleFragment : BaseFragment<FragmentExampleBinding>() { | |
| // FragmentExampleBinding.inflate(...) matches the signature of BindingInflater so we can | |
| // set it as a BindingInflater for the FragmentExampleBinding class | |
| override val bindingInflater: BindingInflater<FragmentExampleBinding> = FragmentExampleBinding::inflate | |
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
| super.onViewCreated(view, savedInstanceState) | |
| with(viewBinding) { | |
| // Set up your views | |
| ... | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment