|
|
|
@ -15,12 +15,12 @@ import com.android.base.kotlin.KtViewHolder |
|
|
|
|
*/ |
|
|
|
|
abstract class SimpleRecyclerAdapter<T>(context: Context, data: List<T>? = null) : RecyclerAdapter<T, KtViewHolder>(context, data) { |
|
|
|
|
|
|
|
|
|
private var mLayoutInflater: LayoutInflater = LayoutInflater.from(mContext) |
|
|
|
|
private var layoutInflater: LayoutInflater = LayoutInflater.from(mContext) |
|
|
|
|
|
|
|
|
|
final override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): KtViewHolder { |
|
|
|
|
val layout = provideLayout(parent, viewType) |
|
|
|
|
val itemView = if (layout is Int) { |
|
|
|
|
mLayoutInflater.inflate(layout, parent, false) |
|
|
|
|
layoutInflater.inflate(layout, parent, false) |
|
|
|
|
} else |
|
|
|
|
layout as View |
|
|
|
|
return KtViewHolder(itemView).apply { |
|
|
|
@ -28,31 +28,15 @@ abstract class SimpleRecyclerAdapter<T>(context: Context, data: List<T>? = null) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected fun onViewHolderCreated(ktViewHolder: KtViewHolder) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
protected open fun onViewHolderCreated(viewHolder: KtViewHolder) = Unit |
|
|
|
|
|
|
|
|
|
/**provide a layout id or a View*/ |
|
|
|
|
abstract fun provideLayout(parent: ViewGroup, viewType: Int): Any |
|
|
|
|
|
|
|
|
|
override fun getItemViewType(position: Int): Int { |
|
|
|
|
return TYPE_ITEM |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun onBindViewHolder(viewHolder: KtViewHolder, position: Int) { |
|
|
|
|
if (viewHolder.itemViewType == TYPE_ITEM) { |
|
|
|
|
bind(viewHolder, getItem(position)) |
|
|
|
|
} else { |
|
|
|
|
bindOtherTypes(viewHolder, position) |
|
|
|
|
} |
|
|
|
|
bind(viewHolder, getItem(position)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected abstract fun bind(viewHolder: KtViewHolder, item: T) |
|
|
|
|
|
|
|
|
|
protected open fun bindOtherTypes(viewHolder: ViewHolder, position: Int) {} |
|
|
|
|
|
|
|
|
|
companion object { |
|
|
|
|
protected const val TYPE_ITEM = 0 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |