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

recyclerview-selection

 recyclerview-selection

at potatotips #50

Shinnosuke Kugimiya

April 19, 2018
Tweet

More Decks by Shinnosuke Kugimiya

Other Decks in Programming

Transcript

  1. CONFIDENTIAL
    © DMM.com
    !LHNZTIJO
    QPUPUJQT
    4VQQPSU-JCSBSZBMQIB
    SFDZDMFSWJFXTFMFDUJPO

    View full-size slide

  2. © DMM.com X
    w LHNZTIJOఝٶ ͗͘Έ΍

    w "OESPJEΤϯδχΞ
    w %..DPNϥϘ$50ࣨॴଐ
    w ೥݄͔Β
    ࣗݾ঺հ

    View full-size slide

  3. © DMM.com X
    BHFOEB

    View full-size slide

  4. © DMM.com X
    w SFDZDMFSWJFXTFMFDUJPOͱ͸ʁ
    w ࢖͍ํ
    w લ४උ
    w 4FMFDUJPO5SBDLFSΛ࡞Δ
    w બ୒ঢ়ଶΛ7JFXʹ൓ө͢Δ
    BHFOEB

    View full-size slide

  5. © DMM.com X
    SFDZDMFSWJFXTFMFDUJPO
    ͱ͸ʁ

    View full-size slide

  6. © DMM.com X
    TVQQPSUMJCSBSZBMQIB͔Β
    BMQIB͔Βಋೖ ΋ͪΖΜQSFWJFXͰ͢

    ઌߦͯ͠ϦϦʔε͞Ε͍ͯΔBOESPJELUYͱಉ͡
    ABOESPJEYAύοέʔδɻ
    ABOESPJEYA͸কདྷͷαϙʔτϥΠϒϥϦͰ࢖༻͞ΕΔ
    ύοέʔδͰ͢ɻ
    SFDZDMFSWJFXTFMFDUJPO͸ͦͷlকདྷͷαϙʔτϥΠϒϥ
    ϦzͷҰͭ

    View full-size slide

  7. © DMM.com X
    ͲΜͳػೳʁ

    View full-size slide

  8. © DMM.com X
    ࢖͍ํ

    View full-size slide

  9. © DMM.com X
    લ४උ
    data class Book(
    val id: Long,
    val title: String,
    val subTitle: String
    )
    ͜ͷ#PPLͷϦετΛෳ਺બ୒Ͱ͖ΔΑ͏ʹ͠·͢

    View full-size slide

  10. © DMM.com X
    લ४උ
    3FDZDMFS7JFXɺ7JFX)PMEFSɺ"EBQUFS͸Ұ୴௨ৗ௨Γ

    ͨͩɺTUBCMF*EΛ࢖͏Α͏ʹ͢Δ
    class BookAdapter(
    context: Context,
    private val bookList: List
    ) : RecyclerView.Adapter() {
    init {
    setHasStableIds(true)
    }
    :
    override fun getItemId(position: Int): Long = bookList[position].id
    :
    }

    View full-size slide

  11. © DMM.com X
    લ४උ
    3FDZDMFS7JFXɺ7JFX)PMEFSɺ"EBQUFS͸Ұ୴௨ৗ௨Γ

    ͨͩɺTUBCMF*EΛ࢖͏Α͏ʹ͢Δ
    class BookAdapter(
    context: Context,
    private val bookList: List
    ) : RecyclerView.Adapter() {
    init {
    setHasStableIds(true)
    }
    :
    override fun getItemId(position: Int): Long = bookList[position].id
    :
    }

    View full-size slide

  12. © DMM.com X
    4FMFDUJPO5SBDLFSΠϯελϯεΛ࡞Δ
    selectionTracker = SelectionTracker.Builder(
    "my-selection-id",
    binding.recyclerView,
    StableIdKeyProvider(binding.recyclerView),
    BookIdDetailsLookup(binding.recyclerView),
    StorageStrategy.createLongStorage())
    .withOnItemActivatedListener { item, e ->
    Log.e("MainActivity", item.toString())
    return@withOnItemActivatedListener true
    }
    .build()

    View full-size slide

  13. © DMM.com X
    4FMFDUJPO5SBDLFSΠϯελϯεΛ࡞Δ
    selectionTracker = SelectionTracker.Builder(
    "my-selection-id",
    binding.recyclerView,
    StableIdKeyProvider(binding.recyclerView),
    BookIdDetailsLookup(binding.recyclerView),
    StorageStrategy.createLongStorage())
    .withOnItemActivatedListener { item, e ->
    Log.e("MainActivity", item.toString())
    return@withOnItemActivatedListener true
    }
    .build()
    ୈҰҾ਺ɻϢχʔΫͳจࣈྻΛɻ
    #VOEMFʹೖΕΔ,FZͱͯ͠࢖ΘΕ·͢ɻ

    View full-size slide

  14. © DMM.com X
    4FMFDUJPO5SBDLFSΠϯελϯεΛ࡞Δ
    selectionTracker = SelectionTracker.Builder(
    "my-selection-id",
    binding.recyclerView,
    StableIdKeyProvider(binding.recyclerView),
    BookIdDetailsLookup(binding.recyclerView),
    StorageStrategy.createLongStorage())
    .withOnItemActivatedListener { item, e ->
    Log.e("MainActivity", item.toString())
    return@withOnItemActivatedListener true
    }
    .build()
    ୈೋҾ਺ɻ
    ֘౰ͷ3FDZDMFS7JFXΛࢦఆ͢Δɻ

    View full-size slide

  15. © DMM.com X
    4FMFDUJPO5SBDLFSΠϯελϯεΛ࡞Δ
    selectionTracker = SelectionTracker.Builder(
    "my-selection-id",
    binding.recyclerView,
    StableIdKeyProvider(binding.recyclerView),
    BookIdDetailsLookup(binding.recyclerView),
    StorageStrategy.createLongStorage())
    .withOnItemActivatedListener { item, e ->
    Log.e("MainActivity", item.toString())
    return@withOnItemActivatedListener true
    }
    .build()
    ୈࡾҾ਺ɻબ୒தͷΞΠςϜͱબ୒தͷLFZͷରԠΛղܾ͢
    Δ*E,FZ1SPWJEFSΛࢦఆ͢Δɻࠓճͩͱ͋ΔTUBCMF*E CPPLJE

    ʹ͍ͨͯ͠ͲΕ͕ର৅ͷ#PPL͔ͷؔ܎Λղܾ͢Δɻ

    View full-size slide

  16. © DMM.com X
    4FMFDUJPO5SBDLFSΠϯελϯεΛ࡞Δ
    selectionTracker = SelectionTracker.Builder(
    "my-selection-id",
    binding.recyclerView,
    StableIdKeyProvider(binding.recyclerView),
    BookIdDetailsLookup(binding.recyclerView),
    StorageStrategy.createLongStorage())
    .withOnItemActivatedListener { item, e ->
    Log.e("MainActivity", item.toString())
    return@withOnItemActivatedListener true
    }
    .build()
    ୈ࢛Ҿ਺ɻ.PUJPO&WFOUΛ΋ͱʹࠓͲ͜ͷΞΠςϜΛબ୒ͯ͠
    Δͷ͔Λղܾ͢ΔɻTUBCMF*E࢖༻࣌͸͚ͩࣗ͜͜࡞ɻ
    TUBCMF*Eඇ࢖༻࣌͸͚ͩ͜͜Ͱͳ͘*E,FZ1SPWJEFS΋ࣗ࡞͢Δɻ

    View full-size slide

  17. © DMM.com X
    4FMFDUJPO5SBDLFSΠϯελϯεΛ࡞Δ
    selectionTracker = SelectionTracker.Builder(
    "my-selection-id",
    binding.recyclerView,
    StableIdKeyProvider(binding.recyclerView),
    BookIdDetailsLookup(binding.recyclerView),
    StorageStrategy.createLongStorage())
    .withOnItemActivatedListener { item, e ->
    Log.e("MainActivity", item.toString())
    return@withOnItemActivatedListener true
    }
    .build()
    ୈޒҾ਺ɻ
    TBWFE4UBUFʹԿΛอ࣋͢Δͷ͔Λղܾ͢Δਓɻ
    *UFNΛ4FMFDUJPO΍#VOEMFʹม׵͢ΔϝιουΛ࣋ͭɻ

    View full-size slide

  18. © DMM.com X
    4FMFDUJPO5SBDLFSΠϯελϯεΛ࡞Δ
    selectionTracker = SelectionTracker.Builder(
    "my-selection-id",
    binding.recyclerView,
    StableIdKeyProvider(binding.recyclerView),
    BookIdDetailsLookup(binding.recyclerView),
    StorageStrategy.createLongStorage())
    .withOnItemActivatedListener { item, e ->
    Log.e("MainActivity", item.toString())
    return@withOnItemActivatedListener true
    }
    .build()
    ඞཁͩͬͨΒϦεφʔొ࿥ɻ

    View full-size slide

  19. © DMM.com X
    #PPL*E%FUBJMT-PPLVQΛ࡞Δ
    class BookIdDetailsLookup(
    private val recyclerView: RecyclerView
    ) : ItemDetailsLookup() {
    override fun getItemDetails(e: MotionEvent): ItemDetails? =
    recyclerView.findChildViewUnder(
    e.x,
    e.y
    )?.let {
    (recyclerView.getChildViewHolder(it) as? BookViewHolder)?.getItemIdDetails()
    }
    }
    ୈࡾҾ਺ͩͬͨ#PPL*E%FUBJMT-PPLVQͷ࣮૷ɻ
    λονΠϕϯτͷ࠲ඪ͔Βɺ֘౰ͷΞΠςϜΛฦ͚ͩ͢
    ͷγϯϓϧͳ࣮૷ɻ

    View full-size slide

  20. © DMM.com X
    ͜͜·Ͱ࣮૷ͨ͠΋ͷΛݟͯΈΔ

    View full-size slide

  21. © DMM.com X
    7JFXʹબ୒ঢ়ଶ͔Ͳ͏͔Λ൓ө͢Δ
    class BookAdapter(
    context: Context,
    private val bookList: List
    ) : RecyclerView.Adapter() {
    ɿ
    override fun onBindViewHolder(
    holder: BookViewHolder,
    position: Int
    ) {
    val item = bookList[position]
    holder.bind(
    selectionChecker?.isSelected(item.id) ?: false,
    position,
    bookList[position]
    )
    }
    }
    TFMFDUPSΛηοτ͓͍ͯͯ͠4FMFDUJPO5SBDLFSJT4FMFDUFEͷ݁ՌΛ
    7JFXTFU"DUJWBUFEͰηοτ͢Δɻ

    View full-size slide

  22. © DMM.com X
    7JFXʹબ୒ঢ়ଶ͔Ͳ͏͔Λ൓ө͢Δ
    class BookAdapter(
    context: Context,
    private val bookList: List
    ) : RecyclerView.Adapter() {
    ɿ
    override fun onBindViewHolder(
    holder: BookViewHolder,
    position: Int
    ) {
    val item = bookList[position]
    holder.bind(
    selectionChecker?.isSelected(item.id) ?: false,
    position,
    bookList[position]
    )
    }
    }
    TFMFDUPSΛηοτ͓͍ͯͯ͠4FMFDUJPO5SBDLFSJT4FMFDUFEͷ݁ՌΛ
    7JFXTFU"DUJWBUFEͰηοτ͢Δɻ

    View full-size slide

  23. © DMM.com X
    ׬੒ʂ

    View full-size slide

  24. © DMM.com X
    ऴΘΓʹ

    View full-size slide

  25. © DMM.com X
    ฐࣾϒϩάͰɺ΋͏গ͠ৄ͘͠঺հͯ͠·͢
    IUUQTJOTJEFENNDPNFOUSZSFDZDMFSWJFXTFMFDUJPO

    View full-size slide

  26. © DMM.com X
    αϯϓϧஔ͍ͯ·͢ɻ
    IUUQTHJUIVCDPNLHNZTIJOSFDZDMFSWJFXTFMFDUJPOTBNQMF
    ANBTUFSATUBCMFJEඇ࢖༻ϒϥϯν
    AVTF@TUBCMF@JEATUBCMFJE࢖༻ϒϥϯν

    View full-size slide

  27. © DMM.com X
    ࠓि೔༵Կͯ͠·͔͢ʁ๩͍͠Ͱ͔͢ʁٕज़ॻయདྷͯ΋Β͍͍ͬͯͰ͔͢ʁ
    ؔ܎ͳ͍಺༰Ͱ͝ΊΜͳ͍͞ʂ
    ,6(*#",0ͱ͍͏αʔΫϧ໊Ͱग़ళ͠·͢ʂ͓࣌ؒ͋Ε͹ੋඇʂ
    IUUQTNPUJEBKBQBOIBUFOBCMPHDPNFOUSZ

    View full-size slide

  28. ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ

    View full-size slide