Last active
January 15, 2021 17:00
-
-
Save ch8n/261de98c23ada764548e3deb7ab8c991 to your computer and use it in GitHub Desktop.
base class fragment using view binding | checkout complete blog @ https://chetangupta.net/viewbinding/
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
// Source : https://chetangupta.net/viewbinding/ | |
// Author : ChetanGupta.net @Androidbites | |
abstract class ViewBindingFragment<VB : ViewBinding> : Fragment() { | |
private var _binding: ViewBinding? = null | |
abstract val bindingInflater: (LayoutInflater, ViewGroup?, Boolean) -> VB | |
@Suppress("UNCHECKED_CAST") | |
protected val binding: VB | |
get() = _binding as VB | |
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
_binding = bindingInflater.invoke(inflater, container, false) | |
return requireNotNull(_binding).root | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
setup() | |
} | |
abstract fun setup() | |
override fun onDestroyView() { | |
super.onDestroyView() | |
_binding = null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment