GoodsTypeRvAdapter.kt左侧的条目绑定点击事件和view选中态的变化
package com.example.takeout.ui.adapter
import android.graphics.Color
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.takeout.R
import com.example.takeout.model.beans.GoodsTypeInfo
import com.example.takeout.ui.fragment.GoodsFragment
import org.jetbrains.anko.find
class GoodsTypeRvAdapter(val context: FragmentActivity?, val goodsFragment: GoodsFragment) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var goodsTypeList: List<GoodsTypeInfo> = listOf()
fun setDatas(list: List<GoodsTypeInfo>) {
this.goodsTypeList = list
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val itemView = LayoutInflater.from(context).inflate(R.layout.item_type, parent, false)
return GoodsTypeItemHolder(itemView)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val goodsTypeItemHolder = holder as GoodsTypeItemHolder
goodsTypeItemHolder.bindData(goodsTypeList.get(position), position)
}
override fun getItemCount(): Int {
return goodsTypeList.size
}
var selectPosition = 0 //选中的位置
inner class GoodsTypeItemHolder(val item: View) : RecyclerView.ViewHolder(item) {
val tvType: TextView
var mPosition: Int = 0
lateinit var goodsTypeInfo: GoodsTypeInfo
init {
tvType = item.find<TextView>(R.id.type)
item.setOnClickListener {
selectPosition = mPosition
notifyDataSetChanged()
//step2:右侧列表跳转到该类型中第一个商品
val typeId = goodsTypeInfo.id
//遍历所有商品,找到此position
val position = goodsFragment.goodsFragmentPresenter.getGoodsPositionByTypeId(typeId)
//右侧粘性listview更新位置
goodsFragment.slhlv.setSelection(position)
}
}
fun bindData(goodsTypeInfo: GoodsTypeInfo, position: Int) {
mPosition = position
this.goodsTypeInfo = goodsTypeInfo
if (position == selectPosition) {
//选中的为白底加粗黑字,
item.setBackgroundColor(Color.WHITE)
tvType.setTextColor(Color.BLACK)
tvType.setTypeface(Typeface.DEFAULT_BOLD)
} else {
//未选中是灰色背景 普通字体
item.setBackgroundColor(Color.parseColor("#b9dedcdc"))
tvType.setTextColor(Color.GRAY)
tvType.setTypeface(Typeface.DEFAULT)
}
tvType.text = goodsTypeInfo.name
}
}
}
GoodsFragmentPresenter.kt获取到左侧的大条目对应到右侧的位置
package com.example.takeout.presenter
import android.util.Log
import com.example.takeout.model.beans.GoodsInfo
import com.example.takeout.model.beans.GoodsTypeInfo
import com.example.takeout.ui.fragment.GoodsFragment
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.json.JSONObject
class GoodsFragmentPresenter(val goodsFragment: GoodsFragment) : NetPresenter() {
var goodstypeList: List<GoodsTypeInfo> = arrayListOf()
val allTypeGoodsList: ArrayList<GoodsInfo> = arrayListOf()
//连接服务器拿到此商家所有商品
fun getBusinessInfo() {
val businessCall = takeoutService.getBusinessInfo()
businessCall.enqueue(callback)
}
override fun parserJson(json: String) {
val gson = Gson()
val jsoObj = JSONObject(json)
val allStr = jsoObj.getString("list")
//商品类型的集合
goodstypeList = gson.fromJson(allStr, object : TypeToken<List<GoodsTypeInfo>>() {
}.type)
Log.e("business", "该商家一共有" + goodstypeList.size + "个类别商品")
for (i in 0 until goodstypeList.size) {
val goodsTypeInfo = goodstypeList.get(i)
var aTypeCount = 0
val aTypeList: List<GoodsInfo> = goodsTypeInfo.list
for (j in 0 until aTypeList.size) {
val goodsInfo = aTypeList.get(j)
//建立双向绑定关系
goodsInfo.typeName = goodsTypeInfo.name
goodsInfo.typeId = goodsTypeInfo.id
}
allTypeGoodsList.addAll(goodsTypeInfo.list)
}
goodsFragment.onLoadBusinessSuccess(goodstypeList, allTypeGoodsList)
}
//根据商品类别id找到此类别第一个商品的位置
fun getGoodsPositionByTypeId(typeId: Int): Int {
var position = -1 //-1表示未找到
for(j in 0 until allTypeGoodsList.size){
val goodsInfo = allTypeGoodsList.get(j)
if(goodsInfo.typeId == typeId){
position = j
break;
}
}
return position
}
}
效果如下: