-
RecyclerView Header , Footer 생성Android 2019. 9. 17. 16:22반응형
https://github.com/DNights/RestAPISample
이전에 제작했던 RestAPISample 코드의 RecyclerView 에 Header 와 Footer 를 추가해보았다.
RecyclerHeader/Footer 라는 Branch로 push 하였습니다.
[원본 코드]
class RecyclerAdapter: RecyclerView.Adapter<RecyclerAdapter.ItemViewHolder>() { private var itemsList = ArrayList<PhotoData>() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.item_imageview, parent, false) return ItemViewHolder(view) } override fun getItemCount() = itemsList.size override fun onBindViewHolder(holder: ItemViewHolder, position: Int) { holder.onBind(itemsList[position]) } fun setList(list: List<PhotoData>){ itemsList = list as ArrayList<PhotoData> } class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { fun onBind(data: PhotoData){ Glide .with(itemView) .load(data.urls.thumb) .into(itemView.findViewById(R.id.imageView_thumb)) } } }
[Header , Footer 추가 코드]
class RecyclerAdapter: RecyclerView.Adapter<RecyclerAdapter.BaseViewHolder>() { private val TYPE_HEADER = 0 private val TYPE_ITEM = 1 private val TYPE_FOOTER = 2 private val HEADER_SIZE = 1 private val FOOTER_SIZE = 1 private var itemsList = ArrayList<PhotoData>() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder = when(viewType){ TYPE_HEADER ->{ val view = LayoutInflater.from(parent.context).inflate(R.layout.item_header, parent, false) HeaderViewHolder(view) } TYPE_FOOTER-> { val view = LayoutInflater.from(parent.context).inflate(R.layout.item_footer, parent, false) FooterViewHolder(view) } TYPE_ITEM->{ val view = LayoutInflater.from(parent.context).inflate(R.layout.item_imageview, parent, false) ItemViewHolder(view) } else -> throw Exception("Unknow viewType $viewType") } override fun getItemCount() = itemsList.size + HEADER_SIZE + FOOTER_SIZE override fun onBindViewHolder(holder: BaseViewHolder, position: Int) { if(holder is ItemViewHolder){ holder.onBind(itemsList[position - HEADER_SIZE]) } } override fun getItemViewType(position: Int): Int = when(position) { 0 -> TYPE_HEADER (itemsList.size + HEADER_SIZE) -> TYPE_FOOTER else -> TYPE_ITEM } fun setList(list: List<PhotoData>){ itemsList = list as ArrayList<PhotoData> } open class BaseViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) class HeaderViewHolder(itemView: View) : BaseViewHolder(itemView) class FooterViewHolder(itemView: View) : BaseViewHolder(itemView) class ItemViewHolder(itemView: View) : BaseViewHolder(itemView) { fun onBind(data: PhotoData){ Glide .with(itemView) .load(data.urls.thumb) .into(itemView.findViewById(R.id.imageView_thumb)) } } }
참고링크
https://dev-imaec.tistory.com/31
반응형'Android' 카테고리의 다른 글
Declarative UI Patterns (0) 2019.10.26 안드로이드 비동기 처리 질문 (Thread Sleep) (0) 2019.10.06 Android Paging Library (0) 2019.10.01 Android Dokka 사용법 (0) 2019.09.23 안드로이드 스레드 (Android Thread) (0) 2019.08.21 안드로이드 런타임 (Android RunTime) (0) 2019.08.10 Droid knights 2019 - 코루틴 안전! 이쁘게 코루틴 사용하는 방법은? (0) 2019.08.06 GitHub - AndroidCheat (0) 2019.07.14