SHARE
TWEET

Untitled

a guest Sep 16th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME}#end
  2.     import android.view.LayoutInflater
  3.     import android.view.View
  4.     import android.view.ViewGroup
  5.     import androidx.recyclerview.widget.DiffUtil
  6.     import androidx.recyclerview.widget.RecyclerView
  7.     import kotlinx.android.extensions.LayoutContainer
  8.     class ${NAME} : RecyclerView.Adapter<${NAME}.${Model}Viewholder>()  {
  9.         var data:List<${Model}> = emptyList()
  10.             set(newList) {
  11.                 val calculateDiff = DiffUtil.calculateDiff(${Model}DiffCallback(field,newList))
  12.                 calculateDiff.dispatchUpdatesTo(this)
  13.                 field = newList
  14.             }
  15.         override fun getItemCount(): Int = data.size
  16.        
  17.         override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ${Model}Viewholder {
  18.             return ${Model}Viewholder(
  19.                     LayoutInflater.from(parent.context)
  20.                             .inflate(R.layout.${Layout}, parent, false)
  21.             )
  22.         }
  23.         override fun onBindViewHolder(holder: ${NAME}.${Model}Viewholder, position: Int) {
  24.             holder.bind(data[position])
  25.         }
  26.         inner class ${Model}Viewholder(override val containerView: View) : RecyclerView.ViewHolder(containerView),
  27.             LayoutContainer {
  28.              fun bind(item: ${Model}) = with(itemView) {
  29.                    
  30.             }
  31.         }
  32.     }
  33.     class ${Model}DiffCallback(val oldList:List<${Model}>,val newList:List<${Model}>) : DiffUtil.Callback() {
  34.         override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
  35.             return oldList[oldItemPosition].id == newList[newItemPosition].id
  36.         }
  37.         override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
  38.             val old = oldList[oldItemPosition]
  39.             val new = newList[newItemPosition]
  40.             return old.id == new.id
  41.         }
  42.         override fun getOldListSize(): Int {
  43.             return oldList.size
  44.         }
  45.         override fun getNewListSize(): Int {
  46.             return newList.size
  47.         }
  48.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top