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. © DMM.com X w LHNZTIJOఝٶ ͗͘Έ΍  w "OESPJEΤϯδχΞ w

    %..DPNϥϘ$50ࣨॴଐ w ೥݄͔Β ࣗݾ঺հ
  2. © DMM.com X w SFDZDMFSWJFXTFMFDUJPOͱ͸ʁ w ࢖͍ํ w લ४උ w

    4FMFDUJPO5SBDLFSΛ࡞Δ w બ୒ঢ়ଶΛ7JFXʹ൓ө͢Δ BHFOEB
  3. © DMM.com X લ४උ data class Book( val id: Long,

    val title: String, val subTitle: String ) ͜ͷ#PPLͷϦετΛෳ਺બ୒Ͱ͖ΔΑ͏ʹ͠·͢
  4. © DMM.com X લ४උ 3FDZDMFS7JFXɺ7JFX)PMEFSɺ"EBQUFS͸Ұ୴௨ৗ௨Γ
 ͨͩɺTUBCMF*EΛ࢖͏Α͏ʹ͢Δ class BookAdapter( context: Context,

    private val bookList: List<Book> ) : RecyclerView.Adapter<BookViewHolder>() { init { setHasStableIds(true) } : override fun getItemId(position: Int): Long = bookList[position].id : }
  5. © DMM.com X લ४උ 3FDZDMFS7JFXɺ7JFX)PMEFSɺ"EBQUFS͸Ұ୴௨ৗ௨Γ
 ͨͩɺTUBCMF*EΛ࢖͏Α͏ʹ͢Δ class BookAdapter( context: Context,

    private val bookList: List<Book> ) : RecyclerView.Adapter<BookViewHolder>() { init { setHasStableIds(true) } : override fun getItemId(position: Int): Long = bookList[position].id : }
  6. © DMM.com X 4FMFDUJPO5SBDLFSΠϯελϯεΛ࡞Δ selectionTracker = SelectionTracker.Builder<Long>( "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()
  7. © DMM.com X 4FMFDUJPO5SBDLFSΠϯελϯεΛ࡞Δ selectionTracker = SelectionTracker.Builder<Long>( "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ͱͯ͠࢖ΘΕ·͢ɻ
  8. © DMM.com X 4FMFDUJPO5SBDLFSΠϯελϯεΛ࡞Δ selectionTracker = SelectionTracker.Builder<Long>( "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Λࢦఆ͢Δɻ
  9. © DMM.com X 4FMFDUJPO5SBDLFSΠϯελϯεΛ࡞Δ selectionTracker = SelectionTracker.Builder<Long>( "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͔ͷؔ܎Λղܾ͢Δɻ
  10. © DMM.com X 4FMFDUJPO5SBDLFSΠϯελϯεΛ࡞Δ selectionTracker = SelectionTracker.Builder<Long>( "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΋ࣗ࡞͢Δɻ
  11. © DMM.com X 4FMFDUJPO5SBDLFSΠϯελϯεΛ࡞Δ selectionTracker = SelectionTracker.Builder<Long>( "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ʹม׵͢ΔϝιουΛ࣋ͭɻ
  12. © DMM.com X 4FMFDUJPO5SBDLFSΠϯελϯεΛ࡞Δ selectionTracker = SelectionTracker.Builder<Long>( "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() ඞཁͩͬͨΒϦεφʔొ࿥ɻ
  13. © DMM.com X #PPL*E%FUBJMT-PPLVQΛ࡞Δ class BookIdDetailsLookup( private val recyclerView: RecyclerView

    ) : ItemDetailsLookup<Long>() { override fun getItemDetails(e: MotionEvent): ItemDetails<Long>? = recyclerView.findChildViewUnder( e.x, e.y )?.let { (recyclerView.getChildViewHolder(it) as? BookViewHolder)?.getItemIdDetails() } } ୈࡾҾ਺ͩͬͨ#PPL*E%FUBJMT-PPLVQͷ࣮૷ɻ λονΠϕϯτͷ࠲ඪ͔Βɺ֘౰ͷΞΠςϜΛฦ͚ͩ͢ ͷγϯϓϧͳ࣮૷ɻ
  14. © DMM.com X 7JFXʹબ୒ঢ়ଶ͔Ͳ͏͔Λ൓ө͢Δ class BookAdapter( context: Context, private val

    bookList: List<Book> ) : RecyclerView.Adapter<BookViewHolder>() { ɿ 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Ͱηοτ͢Δɻ
  15. © DMM.com X 7JFXʹબ୒ঢ়ଶ͔Ͳ͏͔Λ൓ө͢Δ class BookAdapter( context: Context, private val

    bookList: List<Book> ) : RecyclerView.Adapter<BookViewHolder>() { ɿ 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Ͱηοτ͢Δɻ