Upgrade to Pro — share decks privately, control downloads, hide ads and more …

FragmentのInflateを自動で行う / automatically_inflate_...

uhooi
November 17, 2021

FragmentのInflateを自動で行う / automatically_inflate_fragment

[Online] potatotips #82 iOS/Android開発Tips共有会
https://potatotips.connpass.com/event/224395/

## 参考リンク

- View Bindingの公式ドキュメント
https://developer.android.com/topic/libraries/view-binding

- Alternate constructorを使ってInflateしているView Bindingのサンプル
https://github.com/android/architecture-components-samples/blob/master/ViewBindingSample/app/src/main/java/com/android/example/viewbindingsample/BindFragment.kt#L36-L41

- FragmentのInflateを自動で行うPR
https://github.com/uhooi/UhooiPicBook-Android/pull/195

uhooi

November 17, 2021
Tweet

More Decks by uhooi

Other Decks in Programming

Transcript

  1. Fragment の inflate を自動で行う class MainFragment : Fragment() { private

    var _binding: MainFragmentBinding? = null private val binding get() = _binding!! override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { _binding = MainFragmentBinding.inflate( inflater, container, false) return binding.root } }
  2. Fragment の inflate を自動で行う - class MainFragment : Fragment() {

    - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View { - _binding = MainFragmentBinding.inflate( - inflater, container, false) - return binding.root - } + class MainFragment : Fragment(R.layout.main_fragment) { private var _binding: MainFragmentBinding? = null private val binding get() = _binding!! }
  3. Fragment の inflate を自動で行う class MainFragment : Fragment(R.layout.main_fragment) { //

    ... override fun onViewCreated( view: View, savedInstanceState: Bundle? ) { super.onViewCreated(view, savedInstanceState) binding.imageview.setOnClickListener { // ... } } } + val binding = MainFragmentBinding.bind(view)
  4. Fragment の inflate を自動で行う class MainFragment : Fragment(R.layout.main_fragment) { //

    ... } - private var _binding: MainFragmentBinding? = null - private val binding get() = _binding!! - override fun onDestroyView() { - super.onDestroyView() - - _binding = null - }