Last active
August 29, 2017 20:00
-
-
Save jifferon/9662ef297f434fde1d7e73f1c040942a 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 GoodsAdapter (dataSet: MutableList<Good>, val callback: Callback) : AbstractArrayAdapter<Good, GoodsAdapter.BaseHolder>(dataSet) { | |
interface Callback { | |
fun onNormalGoodClick(data: Good) | |
fun onSaleGoodClick(data: Good) | |
} | |
enum class Type(val value: Int) { | |
NORMAL(1), | |
SALE(2) | |
} | |
private val normalCellLayout = R.layout.normal_item | |
private val saleCellLayout = R.layout.sale_item | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseHolder { | |
val inflater = LayoutInflater.from(parent.context) | |
val itemView: View | |
var dataHolder: BaseHolder? = null | |
when(viewType) { | |
Type.NORMAL.value -> { | |
itemView = inflater.inflate(normalCellLayout, parent, false) | |
dataHolder = NormalGoodHolder(itemView, callback) | |
} | |
Type.SALE.value -> { | |
itemView = inflater.inflate(saleCellLayout, parent, false) | |
dataHolder = SaleGoodHolder(itemView, callback) | |
} | |
} | |
return dataHolder!! | |
} | |
override fun getItemViewType(position: Int): Int { | |
if(getItemAtPosition(position).saleAmount > 0) { | |
return Type.SALE.value | |
} | |
return Type.NORMAL.value | |
} | |
override fun onBindViewHolder(holder: BaseHolder, position: Int) { | |
holder.bindData(getItemAtPosition(position)) | |
} | |
abstract class BaseHolder(itemView: View, protected var callback: Callback) : RecyclerView.ViewHolder(itemView) { | |
protected var unbinder: Unbinder? = null | |
protected lateinit var data: Good | |
abstract fun bindData(data: Good) | |
} | |
class SaleGoodHolder(itemView: View, callback: Callback) : BaseHolder(itemView, callback) { | |
@BindView(R.id.sale_item_name) | |
lateinit var name: TextView | |
@BindView(R.id.sale_item_sale_amount) | |
lateinit var saleAmount: TextView | |
@BindView(R.id.sale_item_price) | |
lateinit var price: TextView | |
init { | |
unbinder = ButterKnife.bind(this, itemView) | |
} | |
override fun bindData(data: Good) { | |
this.data = data | |
name.text = data.name | |
saleAmount.text = "${data.saleAmount}" | |
price.text = "${data.calculatePrice()}" | |
itemView.setOnClickListener { | |
callback.onSaleGoodClick(data) | |
} | |
} | |
} | |
class NormalGoodHolder(itemView: View, callback: Callback) : BaseHolder(itemView, callback) { | |
@BindView(R.id.normal_item_name) | |
lateinit var name: TextView | |
@BindView(R.id.normal_item_price) | |
lateinit var price: TextView | |
init { | |
unbinder = ButterKnife.bind(this, itemView) | |
} | |
override fun bindData(data: Good) { | |
this.data = data | |
name.text = data.name | |
price.text = "${data.price}" | |
itemView.setOnClickListener { | |
callback.onNormalGoodClick(data) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment