Groupieやってみよう

 Groupieやってみよう

Android Night 2019/03

4f9d886a40b9e73b3eaf5b460748684a?s=128

Yoshiyasu KO

March 14, 2019
Tweet

Transcript

  1. Groupie΍ͬͯΈΑ͏ ߴɹՅହ Android Night 2019/03

  2. ϓϩϑΟʔϧ • ໊લ: ߴ Յହ (Yoshiyasu KO) • ॴଐ: ϕΨίʔϙϨʔγϣϯ

    • AndroidΤϯδχΞ • : @dashimaki_dofu
  3. ϓϩϑΟʔϧ ʮ͠ΜͪΌΜͷਓʯͱ֮͑ͯؼ͍ͬͯͩ͘͞ • ໊લ: ߴ Յହ (Yoshiyasu KO) • ॴଐ:

    ϕΨίʔϙϨʔγϣϯ • AndroidΤϯδχΞ • : @dashimaki_dofu
  4. ࠓ೔࿩͢͜ͱ • GroupieͬͯԿʁ • Groupieͷ؆୯ͳ࢖͍ํ

  5. GroupieͬͯԿʁ

  6. RecyclerViewΛͬ͘͞ͱ ࣮૷Ͱ͖ΔϥΠϒϥϦ

  7. None
  8. ૉ௚ʹ࣮૷ͯ͠ΈΔ

  9. ૉ௚ʹ࣮૷ͯ͠ΈΔ class HelloWorldViewHolder private constructor(view: View) : RecyclerView.ViewHolder(view) { companion

    object { fun generate(parent: ViewGroup, inflater: LayoutInflater): HelloWorldViewHolder { val view = inflater.inflate(R.layout.hello_world_view, parent, false) return HelloWorldViewHolder(view) } } fun onBind() { /* Կ͔Viewͷૢ࡞Λߦ͏ */ } } ViewHolder ͷ࣮૷ ※ಉ༷ʹ AndroidHelloViewHolder ΋࡞Δ
  10. ૉ௚ʹ࣮૷ͯ͠ΈΔ companion object { const val TYPE_HELLO = 0 const

    val TYPE_ANDROID = 1 } Adapter ͷ࣮૷ - ViewType ͷઃఆ
  11. ૉ௚ʹ࣮૷ͯ͠ΈΔ override fun getItemCount(): Int = items.size Adapter ͷ࣮૷ -

    getItemCount()
  12. ૉ௚ʹ࣮૷ͯ͠ΈΔ override fun getItemViewType(position: Int): Int { val item =

    items[position] as Data return item.type } Adapter ͷ࣮૷ - getViewType()
  13. ૉ௚ʹ࣮૷ͯ͠ΈΔ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : RecyclerView.ViewHolder {

    val inflater = LayoutInflater.from(context) return when (viewType) { TYPE_HELLO -> HelloWorldViewHolder.generate(parent, inflater) TYPE_ANDROID -> AndroidHelloViewHolder.generate(parent, inflater) else -> super.createViewHolder(parent, viewType) } } Adapter ͷ࣮૷ - onCreateViewHolder()
  14. ૉ௚ʹ࣮૷ͯ͠ΈΔ override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { when (holder)

    { is HelloWorldViewHolder -> holder.onBind() is AndroidHelloViewHolder -> holder.onBind() } } Adapter ͷ࣮૷ - onBindViewHolder()
  15. ૉ௚ʹ࣮૷ͯ͠ΈΔ // MainActivity#onCreate val items = (0 until 20).map {

    val type = it % 2 RecyclerAdapter.Data(type) }.toMutableList() val recyclerView = findViewById<RecyclerView>(R.id.recycler_view) recyclerView.layoutManager = LinearLayoutManager(this) recyclerView.adapter = RecyclerAdapter(this, items) RecyclerView ʹ Adapter Λઃఆ
  16. GroupieͩͱͲ͏ͳΔʁ

  17. GroupieͩͱͲ͏ͳΔʁ dependencies { implementation 'com.xwray:groupie:2.3.0' } લ४උ: app/gradle ʹ Groupie

    Λ௥Ճ
  18. GroupieͩͱͲ͏ͳΔʁ class HelloWorldItem : Item<ViewHolder>() { override fun getLayout(): Int

    = R.layout.hello_world_view override fun bind(viewHolder: ViewHolder, position: Int) { /* Կ͔View΁ͷૢ࡞Λߦ͏ */ } } Item ͷ࣮૷ ※ಉ༷ʹ AndroidHelloItem ΋࡞Δ
  19. GroupieͩͱͲ͏ͳΔʁ // GroupieActivity#onCreate val items = (0 until 20).map {

    when (it % 2) { 0 -> HelloWorldItem() 1 -> AndroidHelloItem() else -> HelloWorldItem() } }.toMutableList() val groupAdapter = GroupAdapter<ViewHolder>() recyclerView.adapter = groupAdapter groupAdapter.update(items) GroupAdapter ͷઃఆ
  20. ऴΘΓͰ͢

  21. ؆୯Ͱ͢Ͷ

  22. Կ͕خ͍͠ʁ

  23. Կ͕خ͍͠ʁ 1. ViewHolder ͷ࣮૷ 2. ViewType ͷઃఆ 3. getItemCount() ͷ࣮૷

    4. getViewType() ͷ࣮૷ 5. onCreateViewHolder() ͷ࣮૷ 6. onBindViewHolder() ͷ࣮૷ 7. RecyclerViewʹ Adapter ઃఆ 1. Item ͷ࣮૷ 2. RecyclerViewʹ Adapter ઃఆ σϑΥϧτ (SPVQJF
  24. Կ͕خ͍͠ʁ 1. ViewHolder ͷ࣮૷ 2. ViewType ͷઃఆ 3. getItemCount() ͷ࣮૷

    4. getViewType() ͷ࣮૷ 5. onCreateViewHolder() ͷ࣮૷ 6. onBindViewHolder() ͷ࣮૷ 7. RecyclerViewʹ Adapter ઃఆ 1. Item ͷ࣮૷ 2. RecyclerViewʹ Adapter ઃఆ σϑΥϧτ (SPVQJF
  25. Կ͕خ͍͠ʁ 1. ViewHolder ͷ࣮૷ 2. ViewType ͷઃఆ 3. getItemCount() ͷ࣮૷

    4. getViewType() ͷ࣮૷ 5. onCreateViewHolder() ͷ࣮૷ 6. onBindViewHolder() ͷ࣮૷ 7. RecyclerViewʹ Adapter ઃఆ 1. Item ͷ࣮૷ 2. RecyclerViewʹ Adapter ઃఆ σϑΥϧτ (SPVQJF *UFN͕ʙͷॲཧΛ Ӆṭͯ͠શ෦΍ͬͯ͘ΕΔ
  26. Կ͕خ͍͠ʁ 1. ViewHolder ͷ࣮૷ 2. ViewType ͷઃఆ 3. getItemCount() ͷ࣮૷

    4. getViewType() ͷ࣮૷ 5. onCreateViewHolder() ͷ࣮૷ 6. onBindViewHolder() ͷ࣮૷ 7. RecyclerViewʹ Adapter ઃఆ 1. Item ͷ࣮૷ 2. RecyclerViewʹ Adapter ઃఆ σϑΥϧτ (SPVQJF *UFN͕ʙͷॲཧΛ Ӆṭͯ͠શ෦΍ͬͯ͘ΕΔ ಛʹ͜͜
  27. companion object { const val TYPE_A = 0 const val

    TYPE_B = 1 const val TYPE_C = 2 const val TYPE_D = 3 const val TYPE_E = 4 const val TYPE_F = 5 const val TYPE_G = 6 const val TYPE_H = 7 const val TYPE_I = 8 const val TYPE_J = 9 const val TYPE_K = 10 const val TYPE_L = 11 const val TYPE_M = 12 const val TYPE_N = 13 const val TYPE_O = 14 const val TYPE_P = 15 const val TYPE_Q = 16 const val TYPE_R = 17 const val TYPE_S = 18 const val TYPE_T = 19 const val TYPE_U = 20 const val TYPE_V = 21 const val TYPE_W = 22 const val TYPE_X = 23 const val TYPE_Y = 24 const val TYPE_Z = 25 } ΧΦεͳ7JFX5ZQF
  28. companion object { const val TYPE_A = 0 const val

    TYPE_B = 1 const val TYPE_C = 2 const val TYPE_D = 3 const val TYPE_E = 4 const val TYPE_F = 5 const val TYPE_G = 6 const val TYPE_H = 7 const val TYPE_I = 8 const val TYPE_J = 9 const val TYPE_K = 10 const val TYPE_L = 11 const val TYPE_M = 12 const val TYPE_N = 13 const val TYPE_O = 14 const val TYPE_P = 15 const val TYPE_Q = 16 const val TYPE_R = 17 const val TYPE_S = 18 const val TYPE_T = 19 const val TYPE_U = 20 const val TYPE_V = 21 const val TYPE_W = 22 const val TYPE_X = 23 const val TYPE_Y = 24 const val TYPE_Z = 25 } ❌ ΧΦεͳ7JFX5ZQF
  29. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : RecyclerView.ViewHolder { val

    inflater = LayoutInflater.from(context) return when (viewType) { TYPE_A -> AViewHolder.generate(parent, inflater) TYPE_B -> BViewHolder.generate(parent, inflater) TYPE_C -> CViewHolder.generate(parent, inflater) TYPE_D -> DViewHolder.generate(parent, inflater) TYPE_E -> EViewHolder.generate(parent, inflater) TYPE_F -> FViewHolder.generate(parent, inflater) TYPE_G -> GViewHolder.generate(parent, inflater) TYPE_H -> HViewHolder.generate(parent, inflater) TYPE_I -> IViewHolder.generate(parent, inflater) TYPE_J -> JViewHolder.generate(parent, inflater) TYPE_K -> KViewHolder.generate(parent, inflater) TYPE_L -> LViewHolder.generate(parent, inflater) TYPE_M -> MViewHolder.generate(parent, inflater) // ... // else -> super.createViewHolder(parent, viewType) } } ΧΦεͳ7JFX5ZQFʹҾͬுΒΕͨΧΦεͳϝιουୡ
  30. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : RecyclerView.ViewHolder { val

    inflater = LayoutInflater.from(context) return when (viewType) { TYPE_A -> AViewHolder.generate(parent, inflater) TYPE_B -> BViewHolder.generate(parent, inflater) TYPE_C -> CViewHolder.generate(parent, inflater) TYPE_D -> DViewHolder.generate(parent, inflater) TYPE_E -> EViewHolder.generate(parent, inflater) TYPE_F -> FViewHolder.generate(parent, inflater) TYPE_G -> GViewHolder.generate(parent, inflater) TYPE_H -> HViewHolder.generate(parent, inflater) TYPE_I -> IViewHolder.generate(parent, inflater) TYPE_J -> JViewHolder.generate(parent, inflater) TYPE_K -> KViewHolder.generate(parent, inflater) TYPE_L -> LViewHolder.generate(parent, inflater) TYPE_M -> MViewHolder.generate(parent, inflater) // ... // else -> super.createViewHolder(parent, viewType) } } ❌ ΧΦεͳ7JFX5ZQFʹҾͬுΒΕͨΧΦεͳϝιουୡ
  31. ίʔυྔΊͬͪΌݮΔ = ϝϯςφϯε͠΍͘͢ͳΔ

  32. ͦͷଞͷػೳ • BindingItem<ViewDataBinding> • DataBindingΛར༻Ͱ͖Δ • Section • ϔομ΍ϓϨʔεϗϧμʔΛઃఆͰ͖Δ

  33. ৄ͍͜͠ͱ͸... • Groupie Λ͔ͭͬͯΈΔ • https://qiita.com/aluceps/items/ 86208987f4c07331bfa0 • RecyclerViewʹΑΔϨΠΞ΢τΛָʹ͢ΔGroupieΛ1೥ ࢖ͬͯΈͯ

    • https://qiita.com/takahirom/items/ 4125d7c871fad534d3c2
  34. ࠓ೔࿩ͨ͜͠ͱ • GroupieͬͯԿʁ • RecyclerViewΛͬ͘͞ͱ࣮૷Ͱ͖ΔϥΠϒϥϦ • Ͳ͏ָʹͳΔͷʁ • جຊ͸Item ࡞ͬͯɺGroupAdapterʹઃఆ͢Δ͚ͩ

    • Կ͕خ͍͠ʁ • ViewType ΍ onCreateViewHolder() ͷ࣮૷͕ෆཁ • ίʔυ͕εϦϜʹͳΓɺϝϯςφϯε͠΍͍͢
  35. ͡Ό ͦΏ͜ͱͰʙ