}
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()
}
}
HomeRvAdapter.kt点击跳转到activity以前会先获取缓存的数据
package com.example.takeout.ui.adapter
import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RatingBar
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.daimajia.slider.library.SliderLayout
import com.daimajia.slider.library.SliderTypes.TextSliderView
import com.example.takeout.R
import com.example.takeout.model.beans.Seller
import com.example.takeout.ui.activity.BusinessActivity
import com.example.takeout.utils.TakeoutApp
import com.squareup.picasso.Picasso
import org.jetbrains.anko.find
class HomeRvAdapter(val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
//定义常量
companion object {
val TYPE_TITLE = 0
val TYPE_SELLER = 1
}
val host = “http://127.0.0.1:8090/image?name=”
var mDatas: ArrayList = ArrayList()
fun setData(data: ArrayList) {
this.mDatas = data
notifyDataSetChanged()
}
/**
- 不同position对应不同类型
*/
override fun getItemViewType(position: Int): Int {
if (position == 0) {
return TYPE_TITLE
} else {
return TYPE_SELLER
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val viewType = getItemViewType(position)
when (viewType) {
TYPE_TITLE -> (holder as TitleHolder).bindData(“我是title----------------------------------------”)
TYPE_SELLER -> (holder as SellerHolder).bindData(mDatas[position - 1])
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
when (viewType) {
TYPE_TITLE -> return TitleHolder(View.inflate(context, R.layout.item_title, null))
TYPE_SELLER -> return SellerHolder(View.inflate(context, R.layout.item_seller, null))
else -> return TitleHolder(View.inflate(context, R.layout.item_home_common, null))
}
}
override fun getItemCount(): Int {
if (mDatas.size > 0) {
return mDatas.size + 1
} else {
return 0
}
}
//内部类,商家的holder
inner class SellerHolder(item: View) : RecyclerView.ViewHolder(item) {
val tvTitle: TextView
val ivLogo: ImageView
val rbScore: RatingBar
val tvSale: TextView
val tvSendPrice: TextView
val tvDistance: TextView
lateinit var mSeller: Seller
init {
tvTitle = item.find(R.id.tv_title)
ivLogo = item.find(R.id.seller_logo)
rbScore = item.find(R.id.ratingBar)
tvSale = item.find(R.id.tv_home_sale)
tvSendPrice = item.find(R.id.tv_home_send_price)
tvDistance = item.find(R.id.tv_home_distance)
item.setOnClickListener {
val intent: Intent = Intent(context, BusinessActivity::class.java)//去取小明在田老师这家店是否有缓存信息
//逐层读取,判断整个这家店是否有缓存
var hasSelectInfo = false