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

Groupieやってみよう

 Groupieやってみよう

Android Night 2019/03

Yoshiyasu KO

March 14, 2019
Tweet

More Decks by Yoshiyasu KO

Other Decks in Programming

Transcript

  1. ૉ௚ʹ࣮૷ͯ͠ΈΔ 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 ΋࡞Δ
  2. ૉ௚ʹ࣮૷ͯ͠ΈΔ companion object { const val TYPE_HELLO = 0 const

    val TYPE_ANDROID = 1 } Adapter ͷ࣮૷ - ViewType ͷઃఆ
  3. ૉ௚ʹ࣮૷ͯ͠ΈΔ override fun getItemViewType(position: Int): Int { val item =

    items[position] as Data return item.type } Adapter ͷ࣮૷ - getViewType()
  4. ૉ௚ʹ࣮૷ͯ͠ΈΔ 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()
  5. ૉ௚ʹ࣮૷ͯ͠ΈΔ override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { when (holder)

    { is HelloWorldViewHolder -> holder.onBind() is AndroidHelloViewHolder -> holder.onBind() } } Adapter ͷ࣮૷ - onBindViewHolder()
  6. ૉ௚ʹ࣮૷ͯ͠ΈΔ // 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 Λઃఆ
  7. GroupieͩͱͲ͏ͳΔʁ class HelloWorldItem : Item<ViewHolder>() { override fun getLayout(): Int

    = R.layout.hello_world_view override fun bind(viewHolder: ViewHolder, position: Int) { /* Կ͔View΁ͷૢ࡞Λߦ͏ */ } } Item ͷ࣮૷ ※ಉ༷ʹ AndroidHelloItem ΋࡞Δ
  8. 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 ͷઃఆ
  9. Կ͕خ͍͠ʁ 1. ViewHolder ͷ࣮૷ 2. ViewType ͷઃఆ 3. getItemCount() ͷ࣮૷

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

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

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

    4. getViewType() ͷ࣮૷ 5. onCreateViewHolder() ͷ࣮૷ 6. onBindViewHolder() ͷ࣮૷ 7. RecyclerViewʹ Adapter ઃఆ 1. Item ͷ࣮૷ 2. RecyclerViewʹ Adapter ઃఆ σϑΥϧτ (SPVQJF *UFN͕ʙͷॲཧΛ Ӆṭͯ͠શ෦΍ͬͯ͘ΕΔ ಛʹ͜͜
  13. 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
  14. 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
  15. 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ʹҾͬுΒΕͨΧΦεͳϝιουୡ
  16. 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ʹҾͬுΒΕͨΧΦεͳϝιουୡ
  17. ࠓ೔࿩ͨ͜͠ͱ • GroupieͬͯԿʁ • RecyclerViewΛͬ͘͞ͱ࣮૷Ͱ͖ΔϥΠϒϥϦ • Ͳ͏ָʹͳΔͷʁ • جຊ͸Item ࡞ͬͯɺGroupAdapterʹઃఆ͢Δ͚ͩ

    • Կ͕خ͍͠ʁ • ViewType ΍ onCreateViewHolder() ͷ࣮૷͕ෆཁ • ίʔυ͕εϦϜʹͳΓɺϝϯςφϯε͠΍͍͢