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. Groupie΍ͬͯΈΑ͏
    ߴɹՅହ
    Android Night 2019/03

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  4. ࠓ೔࿩͢͜ͱ
    • GroupieͬͯԿʁ
    • Groupieͷ؆୯ͳ࢖͍ํ

    View full-size slide

  5. GroupieͬͯԿʁ

    View full-size slide

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

    View full-size slide

  7. ૉ௚ʹ࣮૷ͯ͠ΈΔ

    View full-size slide

  8. ૉ௚ʹ࣮૷ͯ͠ΈΔ
    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 ΋࡞Δ

    View full-size slide

  9. ૉ௚ʹ࣮૷ͯ͠ΈΔ
    companion object {
    const val TYPE_HELLO = 0
    const val TYPE_ANDROID = 1
    }
    Adapter ͷ࣮૷ - ViewType ͷઃఆ

    View full-size slide

  10. ૉ௚ʹ࣮૷ͯ͠ΈΔ
    override fun getItemCount(): Int = items.size
    Adapter ͷ࣮૷ - getItemCount()

    View full-size slide

  11. ૉ௚ʹ࣮૷ͯ͠ΈΔ
    override fun getItemViewType(position: Int): Int {
    val item = items[position] as Data
    return item.type
    }
    Adapter ͷ࣮૷ - getViewType()

    View full-size slide

  12. ૉ௚ʹ࣮૷ͯ͠ΈΔ
    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()

    View full-size slide

  13. ૉ௚ʹ࣮૷ͯ͠ΈΔ
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder,
    position: Int) {
    when (holder) {
    is HelloWorldViewHolder -> holder.onBind()
    is AndroidHelloViewHolder -> holder.onBind()
    }
    }
    Adapter ͷ࣮૷ - onBindViewHolder()

    View full-size slide

  14. ૉ௚ʹ࣮૷ͯ͠ΈΔ
    // MainActivity#onCreate
    val items = (0 until 20).map {
    val type = it % 2
    RecyclerAdapter.Data(type)
    }.toMutableList()
    val recyclerView = findViewById(R.id.recycler_view)
    recyclerView.layoutManager = LinearLayoutManager(this)
    recyclerView.adapter = RecyclerAdapter(this, items)
    RecyclerView ʹ Adapter Λઃఆ

    View full-size slide

  15. GroupieͩͱͲ͏ͳΔʁ

    View full-size slide

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

    View full-size slide

  17. GroupieͩͱͲ͏ͳΔʁ
    class HelloWorldItem : Item() {
    override fun getLayout(): Int = R.layout.hello_world_view
    override fun bind(viewHolder: ViewHolder, position: Int) {
    /* Կ͔View΁ͷૢ࡞Λߦ͏ */
    }
    }
    Item ͷ࣮૷
    ※ಉ༷ʹ AndroidHelloItem ΋࡞Δ

    View full-size slide

  18. GroupieͩͱͲ͏ͳΔʁ
    // GroupieActivity#onCreate
    val items = (0 until 20).map {
    when (it % 2) {
    0 -> HelloWorldItem()
    1 -> AndroidHelloItem()
    else -> HelloWorldItem()
    }
    }.toMutableList()
    val groupAdapter = GroupAdapter()
    recyclerView.adapter = groupAdapter
    groupAdapter.update(items)
    GroupAdapter ͷઃఆ

    View full-size slide

  19. Կ͕خ͍͠ʁ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  24. 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

    View full-size slide

  25. 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

    View full-size slide

  26. 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ʹҾͬுΒΕͨΧΦεͳϝιουୡ

    View full-size slide

  27. 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ʹҾͬுΒΕͨΧΦεͳϝιουୡ

    View full-size slide

  28. ίʔυྔΊͬͪΌݮΔ
    = ϝϯςφϯε͠΍͘͢ͳΔ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  31. ࠓ೔࿩ͨ͜͠ͱ
    • GroupieͬͯԿʁ
    • RecyclerViewΛͬ͘͞ͱ࣮૷Ͱ͖ΔϥΠϒϥϦ
    • Ͳ͏ָʹͳΔͷʁ
    • جຊ͸Item ࡞ͬͯɺGroupAdapterʹઃఆ͢Δ͚ͩ
    • Կ͕خ͍͠ʁ
    • ViewType ΍ onCreateViewHolder() ͷ࣮૷͕ෆཁ
    • ίʔυ͕εϦϜʹͳΓɺϝϯςφϯε͠΍͍͢

    View full-size slide

  32. ͡Ό
    ͦΏ͜ͱͰʙ

    View full-size slide