GoodsFragment.kt商品的主界面,使用GoodsTypeRvAdapter的RecycleView来填充左侧的布局
package com.example.takeout.ui.fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.takeout.R
import com.example.takeout.model.beans.GoodsTypeInfo
import com.example.takeout.presenter.GoodsFragmentPresenter
import com.example.takeout.ui.adapter.GoodsTypeRvAdapter
import org.jetbrains.anko.find
/**
* 详情页商品列表界面
*/
class GoodsFragment : Fragment() {
lateinit var rvGoodsType: RecyclerView
lateinit var goodsFragmentPresenter: GoodsFragmentPresenter
lateinit var goodsTypeAdapter :GoodsTypeRvAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val goodsView =
LayoutInflater.from(activity).inflate(R.layout.fragment_goods, container, false)
rvGoodsType = goodsView.find(R.id.rv_goods_type)
rvGoodsType.layoutManager = LinearLayoutManager(activity)
goodsTypeAdapter = GoodsTypeRvAdapter(activity, this)
rvGoodsType.adapter = goodsTypeAdapter
goodsFragmentPresenter = GoodsFragmentPresenter(this)
return goodsView
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
goodsFragmentPresenter.getBusinessInfo()
}
fun onLoadBusinessSuccess(goodstypeList: List<GoodsTypeInfo>) {
goodsTypeAdapter.setDatas(goodstypeList) //左侧列表
goodsTypeAdapter.notifyDataSetChanged()
}
}
GoodsFragmentPresenter.kt数据请求的
package com.example.takeout.presenter
import android.util.Log
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()
//连接服务器拿到此商家所有商品
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 + "个类别商品")
goodsFragment.onLoadBusinessSuccess(goodstypeList)
}
}
GoodsTypeInfo.kt商品的bean
package com.example.takeout.model.beans
class GoodsTypeInfo {
var id: Int = 0//商品类型id
var name: String = ""//商品类型名称
var info: String = ""//特价信息
var list: List<GoodsInfo> = listOf()//商品列表
constructor() : super() {}
constructor(id: Int, name: String, info: String, list: List<GoodsInfo>) : super() {
this.id = id
this.name = name
this.info = info
this.list = list
}
override fun toString(): String {
return "GoodsTypeInfo [id=$id, name=$name, info=$info, list=$list]"
}
var redDotCount: Int = 0
}
GoodsTypeRvAdapter.kt商品左侧主界面的RecycleView,其中使用FragmentActivity?来作为androidx下面的Context
package com.example.takeout.ui.adapter
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
}
inner class GoodsTypeItemHolder(val item: View) : RecyclerView.ViewHolder(item) {
val tvType: TextView
init {
tvType = item.find<TextView>(R.id.type)
}
fun bindData(goodsTypeInfo: GoodsTypeInfo, position: Int) {
tvType.text = goodsTypeInfo.name
}
}
}
TakeoutService.kt服务端的请求格式
package com.example.takeout.model.net
import retrofit2.Call
import retrofit2.http.GET
import rx.Observable
interface TakeoutService {
//ex. @GET("users/{user}/repos")
//ex. fun listRepos(@Path("user") user: String): Call<List<Repo>>
//http://127.0.0.1:8090/takeout?index=0
@GET("takeout?index=0")
fun getHomeInfo(): Call<ResponseInfo>
//http://127.0.0.1:8090/takelogin?index=0
@GET("takelogin?index=0")
fun loginByPhone(): Call<ResponseInfo1>
//http://127.0.0.1:8090/takeorder?index=0
@GET("takeorder?index=0")
fun getOrderList(): Call<ResponseInfo1>
//使用Rxjava组合的接口
//http://127.0.0.1:8090/takeorder?index=0
@GET("takeorder?index=0")
fun getOrderListByRxjava(): Observable<ResponseInfo1>
@GET("takebusiness?index=0")
fun getBusinessInfo(): Call<ResponseInfo1>
}
item_type.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:clickable="true"
android:layout_height="wrap_content"
android:minHeight="80dp"
android:background="#b9dedcdc"
android:orientation="vertical">
<TextView android:gravity="center"
android:id="@+id/tvRedDotCount"
android:layout_marginTop="5dp"
android:textColor="#fff"
android:text="1"
android:visibility="invisible"
android:layout_marginRight="5dp"
android:textSize="12sp"
android:background="@drawable/circle_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="5dp"
android:id="@+id/type"
android:text="种类1">
</TextView>
</LinearLayout>
效果如下: