GDG Android - Android Adapter 어떻게 사용하는게 좋을까?

7589a5a8fec022e8af4e46525150a291?s=47 TaeHwan
February 03, 2018

GDG Android - Android Adapter 어떻게 사용하는게 좋을까?

2018/02/02 GDG Android Lightning Talk Announcement

7589a5a8fec022e8af4e46525150a291?s=128

TaeHwan

February 03, 2018
Tweet

Transcript

  1. 5.

    ੉ۧѱ ࢎਊೞભ? 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) }
  2. 6.

    N ѐ੄ ViewHolderо ೙ਃೞ׮ class ViewHolderOne(itemView: View) : RecyclerView.ViewHolder(itemView) class

    ViewHolderTwo(itemView: View) : RecyclerView.ViewHolder(itemView) class ViewHolderThree(itemView: View) : RecyclerView.ViewHolder(itemView)
  3. 7.

    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 } } }
  4. 8.

    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?) }
  5. 9.

    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)) } // ࢤۚ }
  6. 10.

    MVP ܻ࠙о оמೠо? थ਌ש੄ Ӗী ٮܲ ܻ࠙ — ViewHolder :

    Viewਸ Ӓܻח ৉ೡ݅ ׸׼ — View : Viewী ؀ೠ interface ੿੄ — Model : Modelী ؀ೠ interface ੿੄ -> Adapter : ୡӝച ߂ View/Model interface ࢚ࣘਸ ҳഅ -> ৻ࠗ੄ Presenterীࢲ пп੄ interface ഐ୹
  7. 11.

    ܻ࠙о оמೠо? interface ImageViewAdapterContract { interface View { val onItemTouchListener:

    OnItemTouchListener? fun reload() } interface Model { fun addItem(item: RecentPhotoItem?) fun getItem(position: Int): RecentPhotoItem fun clear() } }
  8. 12.

    ܻ࠙о оמೠо? 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() } }
  9. 14.

    ViewModelਸ بੑ೧ࠅө? — ௼ѱ ׳ۄ૑ח ࠗ࠙਷ হ૑݅ ViewModel۽ ҳࢿೞҊ र׮

    — Adapterীࢲח onCreateViewHolder݅ ೞҊ र׮ — BaseAdapter ੿੄݅ ੜ೧ࢲ ୊ܻೞҊ र׮ — Modelਸ ߹ب۽ ҙܻೠ׮ -> Adapter੄ ৉ೡ਷ ߹Ѣ হ׮
  10. 15.

    ViewModel ੌױ ٜ݅੗ interface BaseAdapterNavigator { fun getItemViewType(position: Int): Int

    fun addItem(item: Any?, viewType: Int) fun clear() fun getItemCount(): Int }
  11. 16.

    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 }
  12. 17.

    item੄ ਬোೠ ࢎਊਸ ਤ೧ ViewType/ӝఋ ੿ࠁܳ ਬোೞѱ о૑۰Ҋ, ইې੄ sealed

    class ੿ ੄ sealed class ViewHolderSealedData data class ViewHolderInfo( val viewType: Int) : ViewHolderSealedData()
  13. 18.

    Adapterীࢲ Model ੿੄ܳ ܻ࠙ — Adapterীࢲ Model ੿੄ ܻ࠙ —

    Adapterח ԇؘӝܳ ࢤࢿೞח ৉ೡ੉ ੹ࠗ (onCreateViewHolder) — ViewHolderীࢲب BaseAdapterNavigatorਸ ా೧ࢲ ੽Ӕ — ߡౡ ௿ܼ ৉द Navigator ੿੄ܳ ా೧ ੽Ӕ — ೙ਃী ٮۄ Adapter੄ ӝࠄ ViewModel ੤੿੄೧ࢲ ࢎਊ
  14. 19.

    Adapter ୡӝചח? private val adapter: BaseRecyclerAdapter by lazy { object

    : BaseRecyclerAdapter(this, BaseAdapterViewModel(this)) { override fun onCreateViewHolder( parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder { // ࢤۚ } } }
  15. 20.

    ؊ જ਷ ߑध੄ Adapter ੽Ӕߨ? — Adapter ࢚ࣘ ҳഅਸ ୭ࣗച

    दఃӝ ਤೣ — ViewModel ୡӝച݅ ೞৈ ࢎਊ оמ — ViewHolderীࢲח View੄ ৉ೡ݅ ೞب۾ ୊ܻ -> ߈ࠂ ௏٬ਸ ઴੉ӝ ਤೣ