Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
GDG Android - Android Adapter 어떻게 사용하는게 좋을까?
Search
TaeHwan
February 03, 2018
Programming
2
24k
GDG Android - Android Adapter 어떻게 사용하는게 좋을까?
2018/02/02 GDG Android Lightning Talk Announcement
TaeHwan
February 03, 2018
Tweet
Share
More Decks by TaeHwan
See All by TaeHwan
Compose 함수 나누는 조건은?
taehwandev
0
1.2k
Android MVVM 패턴의 접근법 - 2023 드로이드 나이츠
taehwandev
1
1.6k
안드로이드의 변화 - IO Extended 2023 - 송도
taehwandev
0
520
What’s new in Android?(Google IO ex 23-GDG Pangyo)
taehwandev
0
810
Jetpack Compose 상태 및 사이드 효과
taehwandev
0
1.1k
GDG DevFest2022 Songdo - KMM(Kotlin Multiplatform Mobile)
taehwandev
0
550
Kotlin Night 2022 - 코틀린 어노테이션으로 할 수 있는 것(GDG Seoul)
taehwandev
2
1.6k
Android module 개발 - Now in android 참고
taehwandev
3
16k
Android Compose Component - mapping.
taehwandev
2
4.5k
Other Decks in Programming
See All in Programming
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
340
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.5k
イベント駆動で成長して委員会
happymana
1
320
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
290
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
900
Featured
See All Featured
Designing for humans not robots
tammielis
250
25k
Ruby is Unlike a Banana
tanoku
97
11k
Producing Creativity
orderedlist
PRO
341
39k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Designing for Performance
lara
604
68k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
Navigating Team Friction
lara
183
14k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
Scaling GitHub
holman
458
140k
Transcript
Adapter যڌѱ Ӕೞݶ જਸө? Taehwan
ViewModel ਊೞৈ ܻ࠙ೞӝ
ೞ݅ ৈӝࢲࠗఠ द — SeongUg Steve Jung(GDE) : Adapter, ־ҳջ
ք? — Data? View?
Adapter ۧѱ ҳࢿغযભ?
ۧѱ ࢎਊೞભ? class SimpleAdapter(private val context: Context, private val list:
ArrayList<String>) : RecyclerView.Adapter<SimpleAdapter.ViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): SimpleAdapter.ViewHolder { return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_text_sample, parent)) } override fun getItemCount() = list.size override fun onBindViewHolder(holder: SimpleAdapter.ViewHolder?, position: Int) { holder?.itemView?.run { tv_message.text = "Set TEXT" } } class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) }
N ѐ ViewHolderо ਃೞ class ViewHolderOne(itemView: View) : RecyclerView.ViewHolder(itemView) class
ViewHolderTwo(itemView: View) : RecyclerView.ViewHolder(itemView) class ViewHolderThree(itemView: View) : RecyclerView.ViewHolder(itemView)
N ѐ ViewHolderо ਃೞ class SimpleAdapter(private val context: Context, private
val list: ArrayList<String>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder { return when (viewType) { TYPE_ONE -> ViewHolderOne(LayoutInflater.from(context).inflate(R.layout.item_text_sample, parent)) } } override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) { when (holder) { is ViewHolderOne -> { holder.itemView?.run { tv_message.text = "ViewHolder One" } } } } override fun getItemViewType(position: Int): Int { return when (position) { else -> TYPE_ONE } } }
ViewHolder по ܻೞب۾ abstract class BaseViewHolder<in ITEM : Any>( val
context: Context, @LayoutRes layoutRes: Int, parent: ViewGroup?) : RecyclerView.ViewHolder(LayoutInflater.from(context).inflate(layoutRes, parent, false)) { fun onBindViewHolder(item: Any?) { try { onViewCreated(item as? ITEM?) } catch (e: Exception) { itemView.visibility = View.GONE } } abstract fun onViewCreated(item: ITEM?) }
BaseRecyclerAdapterীࢲب ਃۧѱ abstract class BaseRecyclerAdapter(val context: Context?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) { (holder as? BaseViewHolder<*>)?.onBindViewHolder(getItem(position)) } // ࢤۚ }
MVP ܻ࠙о оמೠо? थש Ӗী ٮܲ ܻ࠙ — ViewHolder :
Viewਸ Ӓܻח ೡ݅ — View : Viewী ೠ interface — Model : Modelী ೠ interface -> Adapter : ୡӝച ߂ View/Model interface ࢚ࣘਸ ҳഅ -> ৻ࠗ Presenterীࢲ пп interface ഐ
ܻ࠙о оמೠо? interface ImageViewAdapterContract { interface View { val onItemTouchListener:
OnItemTouchListener? fun reload() } interface Model { fun addItem(item: RecentPhotoItem?) fun getItem(position: Int): RecentPhotoItem fun clear() } }
ܻ࠙о оמೠо? class ImageAdapter(private val context: Context) : RecyclerView.Adapter<BaseViewHolder<RecentPhotoItem>>(), ImageViewAdapterContract.View,
ImageViewAdapterContract.Model { val itemList: ArrayList<RecentPhotoItem> = ArrayList() override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int) = when (viewType) { VIEW_TYPE_ASYNC -> ImageAsyncViewHolder(context, parent, onItemTouchListener) } override fun onBindViewHolder(holder: BaseViewHolder<RecentPhotoItem>?, position: Int) { holder?.bindView(getItem(position), position) } override fun getItemViewType(position: Int) = getItem(position).viewType override fun addItem(item: RecentPhotoItem?) { item?.let { itemList.add(it) } } override fun getItem(position: Int) = itemList[position] override fun reload() { notifyDataSetChanged() } }
Ӓ۞ݶ ViewModel۽ ֈযо ࠁ
ViewModelਸ بੑ೧ࠅө? — ѱ ׳ۄח ࠗ࠙ হ݅ ViewModel۽ ҳࢿೞҊ र
— Adapterীࢲח onCreateViewHolder݅ ೞҊ र — BaseAdapter ݅ ੜ೧ࢲ ܻೞҊ र — Modelਸ ߹ب۽ ҙܻೠ -> Adapter ೡ ߹Ѣ হ
ViewModel ੌױ ٜ݅ interface BaseAdapterNavigator { fun getItemViewType(position: Int): Int
fun addItem(item: Any?, viewType: Int) fun clear() fun getItemCount(): Int }
ViewModel ੌױ ٜ݅ open class BaseAdapterViewModel(override val context: Context?) :
BaseAdapterNavigator { protected val itemList = mutableListOf<Pair<Any?, ViewHolderSealedData>>() override fun getItemViewType(position: Int): Int { val item = getPairItem(position) return when (item) { is ViewHolderInfo -> item.viewType else -> -1 } } override fun addItem(item: Any?, viewType: Int) { itemList.add(Pair(item, ViewHolderInfo(viewType))) } override fun clear() { itemList.clear() } override fun getItemCount() = itemList.size }
item ਬোೠ ࢎਊਸ ਤ೧ ViewType/ӝఋ ࠁܳ ਬোೞѱ о۰Ҋ, ইې sealed
class sealed class ViewHolderSealedData data class ViewHolderInfo( val viewType: Int) : ViewHolderSealedData()
Adapterীࢲ Model ܳ ܻ࠙ — Adapterীࢲ Model ܻ࠙ —
Adapterח ԇؘӝܳ ࢤࢿೞח ೡ ࠗ (onCreateViewHolder) — ViewHolderীࢲب BaseAdapterNavigatorਸ ా೧ࢲ Ӕ — ߡౡ ܼ द Navigator ܳ ా೧ Ӕ — ਃী ٮۄ Adapter ӝࠄ ViewModel ೧ࢲ ࢎਊ
Adapter ୡӝചח? private val adapter: BaseRecyclerAdapter by lazy { object
: BaseRecyclerAdapter(this, BaseAdapterViewModel(this)) { override fun onCreateViewHolder( parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder { // ࢤۚ } } }
؊ જ ߑध Adapter Ӕߨ? — Adapter ࢚ࣘ ҳഅਸ ୭ࣗച
दఃӝ ਤೣ — ViewModel ୡӝച݅ ೞৈ ࢎਊ оמ — ViewHolderীࢲח View ೡ݅ ೞب۾ ܻ -> ߈ࠂ ٬ਸ ӝ ਤೣ