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
510
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
推し活の ハイトラフィックに立ち向かう Railsとアーキテクチャ - Kaigi on Rails 2024
falcon8823
6
2.4k
VR HMDとしてのVision Pro+ゲーム開発について
yasei_no_otoko
0
110
Progressive Web Apps für Desktop und Mobile mit Angular (Hands-on)
christianliebel
PRO
0
110
Kubernetes for Data Engineers: Building Scalable, Reliable Data Pipelines
sucitw
1
200
Realtime API 入門
riofujimon
0
120
Amazon Qを使ってIaCを触ろう!
maruto
0
230
飲食業界向けマルチプロダクトを実現させる開発体制とリアルな現状
hiroya0601
1
400
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
650
カラム追加で増えるActiveRecordのメモリサイズ イメージできますか?
asayamakk
4
1.7k
破壊せよ!データ破壊駆動で考えるドメインモデリング / data-destroy-driven
minodriven
16
4.1k
Modern Angular: Renovation for Your Applications
manfredsteyer
PRO
0
220
go.mod、DockerfileやCI設定に分散しがちなGoのバージョンをまとめて管理する / Go Connect #3
arthur1
10
2.4k
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
297
20k
The Invisible Side of Design
smashingmag
297
50k
For a Future-Friendly Web
brad_frost
175
9.4k
Designing Experiences People Love
moore
138
23k
Faster Mobile Websites
deanohume
304
30k
KATA
mclloyd
29
13k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
What's new in Ruby 2.0
geeforr
343
31k
How to Ace a Technical Interview
jacobian
275
23k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
Gamification - CAS2011
davidbonilla
80
5k
The Cult of Friendly URLs
andyhume
78
6k
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 ೡ݅ ೞب۾ ܻ -> ߈ࠂ ٬ਸ ӝ ਤೣ