takeoutService.getHomeInfo().enqueue(object : Callback {
override fun onResponse(call: Call?, response: Response?) {
if (response == null) {
Log.d(“HomeFragmentPresenster”, “服务器没有成功返回=====”)
} else {
if (response.isSuccessful()) {
val responseInfo = response.body()
val nearbySellerList: List = responseInfo.nearbySellerList
val otherSellerList: List = responseInfo.otherSellerList
//有数据,成功页面
if (nearbySellerList.isNotEmpty() || otherSellerList.isNotEmpty()) {
homeFragment.onHomeSuccess(nearbySellerList, otherSellerList)
} else {
//无数据,异常页面
homeFragment.onHomeFailed()
}
}
}
}
override fun onFailure(call: Call?, t: Throwable?) {
//失败
Log.d(“HomeFragmentPresenster”, “onFailure=====” + t.toString())
}
})
}
}
HomeRvAdapter.kt负责adapter每一个条目的刷新,这里对于大list条目只是设置了一个TextView
package com.example.takeout.ui.adapter
import android.content.Context
import android.view.View
import android.view.ViewGroup
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.heima.takeout.model.beans.Seller
import org.jetbrains.anko.find
class HomeRvAdapter(val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
//定义常量
companion object {
val TYPE_TITLE = 0
val TYPE_SELLER = 1
}
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_S