Created
February 23, 2022 23:27
-
-
Save akndmr/7cd4d7fc781ebef18203cebd96d34c58 to your computer and use it in GitHub Desktop.
BaseFragment for ViewBinding, BaseViewBinding
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
abstract class BaseFragment<VB: ViewBinding, VM : BaseViewModel>( | |
private val bindingInflater: (LayoutInflater) -> VB | |
) : Fragment() { | |
private var _binding: VB? = null | |
open val binding: VB | |
get() = _binding as VB | |
override fun onCreateView( | |
inflater: LayoutInflater, | |
container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
_binding = bindingInflater.invoke(inflater) | |
_binding?.let { | |
return it.root | |
} ?: throw IllegalArgumentException("Binding variable is null") | |
} | |
} |
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
class OnboardingFragment : | |
BaseFragment<FragmentOnboardingBinding, OnboardingViewModel>(FragmentOnboardingBinding::inflate) { | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
binding.textviewSample.text = "viewbinding" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment