abstract class BaseRecyclerViewAdapter<T, VDB : ViewDataBinding>(var items: List<T>) :
RecyclerView.Adapter<BaseRecyclerViewAdapter<T,VDB>.BaseViewHolder>(){
@LayoutRes
abstract fun getLayoutResId(): Int
var binding: Any? = null
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): BaseRecyclerViewAdapter<T, VDB>.BaseViewHolder {
return BaseViewHolder(
LayoutInflater.from(parent.context).inflate(
getLayoutResId(),
parent,
false
)
)
}
override fun getItemCount() = items.size
open inner class BaseViewHolder(view : View) : BindingViewHolder<VDB>(view)
}
https://jungwoon.github.io/android/util/2020/10/05/BaseRecyclerViewAdapter.html
이 블로그를 참조해서 만들었다.
abstract class PagingRecyclerViewAdapter<T : Any, VDB : ViewDataBinding>(var items: List<T>) :
PagingDataAdapter<T, PagingRecyclerViewAdapter<T,VDB>.BaseViewHolder>(object :
DiffUtil.ItemCallback<T>() {
override fun areItemsTheSame(oldItem: T, newItem: T): Boolean =
oldItem == newItem
override fun areContentsTheSame(oldItem: T, newItem: T): Boolean =
areItemsTheSame(oldItem, newItem)
}){
@LayoutRes
abstract fun getLayoutResId(): Int
var binding: Any? = null
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): PagingRecyclerViewAdapter<T, VDB>.BaseViewHolder {
return BaseViewHolder(
LayoutInflater.from(parent.context).inflate(
getLayoutResId(),
parent,
false
)
)
}
override fun getItemCount() = items.size
inner class BaseViewHolder(view : View) : BindingViewHolder<VDB>(view)
}
여기서 Paging3에 적용하려면 이런식으로
'프로그램 > android' 카테고리의 다른 글
안드로이드 Jetpack (0) | 2022.07.09 |
---|---|
안드로이드 스튜디오 설치 (0) | 2021.11.01 |
댓글