Last active
September 30, 2019 08:57
-
-
Save hjiee/dc0149c3ffd83000a980f869785c2d7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomViewHolder : BaseViewHolder<CustomViewHolder>() { | |
private var tvTitle : TextView? = null | |
private var tvItem : TextView? = null | |
override fun onCreate() : CustomViewHolder = CustomViewHolder() | |
override fun onBind(view : View): CustomViewHolder { | |
tvTitle = view.tv_title | |
tvItem = view.tv_item | |
return this | |
} | |
override fun <ITEM> onSet(item: ITEM) { | |
when(item) { | |
is Item -> { | |
tvTitle?.text = item.title | |
tvItem?.text = item.item | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val adapter = BaseListViewAdapter<Information,CustomViewHolder> ( | |
R.layout.listview_item, | |
CustomViewHolder() | |
) | |
adapter.replaceAll(createList()) | |
lv_list.adapter = adapter | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseListViewAdapter<ITEM : Any, VH : BaseViewHolder<VH>>( | |
private val layoutResId: Int, | |
private val items: List<ITEM>, | |
private var viewHolder: VH | |
) : BaseAdapter() { | |
// ...중략 | |
override fun getView(position: Int, view: View?, parent: ViewGroup?): View? { | |
var convertView = view | |
when (convertView) { | |
null -> { | |
convertView = LayoutInflater.from(parent?.context).inflate(layoutResId, parent, false) | |
viewHolder = (viewHolder).onCreate() | |
convertView.tag = viewHolder.onBind(convertView) | |
} | |
else -> { | |
viewHolder = convertView.tag as VH | |
} | |
} | |
viewHolder.onSet(items[position]) | |
return convertView | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class BaseViewHolder<VH> { | |
abstract fun onCreate(): VH // ViewHolder를 생성한다. | |
abstract fun onBind(view: View): VH // findViewById로 리소스를 바인딩한다. | |
abstract fun <ITEM> onSet(item: ITEM) // View의 값을 변경한다. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment