Save 37% off PRO during our Black Friday Sale! »

FragmentのInflateを自動で行う / automatically_inflate_fragment

42a6a049ac8f5265f31858a9509217fb?s=47 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

42a6a049ac8f5265f31858a9509217fb?s=128

uhooi

November 17, 2021
Tweet

Transcript

  1. 2021.11.17(Wed) potatotips #76 Android 発表枠 @the_uhooi Fragment の inflate を で行う

    自動
  2. Mobile app developer @uhooi @uhooi @the_uhooi

  3. PICTURE BOOK MONSTER ORIGINAL 累計100万DL(予定) 主要画面数2

  4. 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 } }
  5. Fragment の inflate を自動で行う 引用: https://developer.android.com/topic/libraries/view-binding

  6. Fragment の inflate を自動で行う 引用: https://github.com/android/architecture-components-samples/blob/master/ViewBindingSample/app/src/main/java/com/android/example/viewbindingsample/BindFragment.kt#L36-L41

  7. 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!! }
  8. 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)
  9. 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 - }
  10. ・Fragment にレイアウトを渡すと自動で inflate する ・必要なければフィールドに binding を保持しない まとめ