GoodsAdapter.kt界面的元素的填充和完善
package com.example.takeout.ui.adapter
import android.graphics.Color
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import com.example.takeout.R
import com.example.takeout.model.beans.GoodsInfo
import com.example.takeout.ui.fragment.GoodsFragment
import com.example.takeout.utils.PriceFormater
import com.squareup.picasso.Picasso
import org.jetbrains.anko.find
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter
class GoodsAdapter(val context: FragmentActivity?, val goodsFragment: GoodsFragment) : BaseAdapter(), StickyListHeadersAdapter {
val host = "http://127.0.0.1:8090/image?name="
var goodsList: List<GoodsInfo> = ArrayList()
fun setDatas(goodsInfoList: List<GoodsInfo>) {
this.goodsList = goodsInfoList
notifyDataSetChanged()
}
inner class GoodsItemHolder(itemView: View) : View.OnClickListener {
override fun onClick(v: View?) {
TODO("Not yet implemented")
}
val ivIcon: ImageView
val tvName: TextView
val tvForm: TextView
val tvMonthSale: TextView
val tvNewPrice: TextView
val tvOldPrice: TextView
val btnAdd: ImageButton
val btnMinus: ImageButton
val tvCount: TextView
lateinit var goodsInfo: GoodsInfo
init {
ivIcon = itemView.find(R.id.iv_icon)
tvName = itemView.find(R.id.tv_name)
tvForm = itemView.find(R.id.tv_form)
tvMonthSale = itemView.find(R.id.tv_month_sale)
tvNewPrice = itemView.find(R.id.tv_newprice)
tvOldPrice = itemView.find(R.id.tv_oldprice)
tvCount = itemView.find(R.id.tv_count)
btnAdd = itemView.find(R.id.ib_add)
btnMinus = itemView.find(R.id.ib_minus)
btnAdd.setOnClickListener(this)
btnMinus.setOnClickListener(this)
}
fun bindData(goodsInfo: GoodsInfo) {
this.goodsInfo = goodsInfo
//图片路径http://127.0.0.1:8090/image?name=takeout/businessimg/goods/0.jpg
Picasso.with(context).load(host + goodsInfo.icon).into(ivIcon)
tvName.text = goodsInfo.name
// tvForm.text = goodsInfo.form
tvMonthSale.text = "月售${goodsInfo.monthSaleNum}份"
tvNewPrice.text = PriceFormater.format(goodsInfo.newPrice.toFloat())
// tvNewPrice.text = "$${goodsInfo.newPrice}"
tvOldPrice.text = "¥${goodsInfo.oldPrice}"
tvOldPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
if (goodsInfo.oldPrice > 0) {
tvOldPrice.visibility = View.VISIBLE
} else {
tvOldPrice.visibility = View.GONE
}
// tvCount.text = goodsInfo.count.toString()
if (goodsInfo.count > 0) {
tvCount.visibility = View.VISIBLE
btnMinus.visibility = View.VISIBLE
} else {
tvCount.visibility = View.INVISIBLE
btnMinus.visibility = View.INVISIBLE
}
}
}
override fun getCount(): Int {
return goodsList.size
}
override fun getItem(position: Int): Any {
return goodsList.get(position)
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
var itemView: View
val goodsItemHolder: GoodsItemHolder
if (convertView == null) {
itemView = LayoutInflater.from(context).inflate(R.layout.item_goods, parent, false)
goodsItemHolder = GoodsItemHolder(itemView)
itemView.tag = goodsItemHolder
} else {
itemView = convertView
goodsItemHolder = convertView.tag as GoodsItemHolder
}
goodsItemHolder.bindData(goodsList.get(position))
return itemView
}
override fun getHeaderView(position: Int, convertView: View?, parent: ViewGroup?): View {
val goodsInfo: GoodsInfo = goodsList.get(position)
val typeName = goodsInfo.typeName
val textView: TextView = LayoutInflater.from(context).inflate(R.layout.item_type_header, parent, false) as TextView
textView.text = typeName
textView.setTextColor(Color.BLACK)
return textView
}
override fun getHeaderId(position: Int): Long {
val goodsInfo: GoodsInfo = goodsList.get(position)
return goodsInfo.typeId.toLong()
}
}
PriceFormater.kt价格转换
package com.example.takeout.utils
import java.text.NumberFormat
object PriceFormater {
fun format(countPrice: Float): String {
val format = NumberFormat.getCurrencyInstance()
format.maximumFractionDigits = 2
return format.format(countPrice.toDouble())
}
}
效果如下: