Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Mobile app developer @uhooi @uhooi @the_uhooi

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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 } }

Slide 5

Slide 5 text

Fragment の inflate を自動で行う 引用: https://developer.android.com/topic/libraries/view-binding

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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!! }

Slide 8

Slide 8 text

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)

Slide 9

Slide 9 text

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 - }

Slide 10

Slide 10 text

・Fragment にレイアウトを渡すと自動で inflate する ・必要なければフィールドに binding を保持しない まとめ