OrderFragment.kt订单的主界面,与presenter交互,请求网络数据并通过RecycleView进行填充数据并展示
package com.example.takeout.ui.fragment
import android.app.Fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.takeout.R
import com.example.takeout.model.beans.Order
import com.example.takeout.presenter.OrderFragmentPresenter
import com.example.takeout.ui.adapter.OrderRvAdapter
import com.example.takeout.utils.TakeoutApp
import org.jetbrains.anko.find
import org.jetbrains.anko.toast
class OrderFragment : Fragment() {
lateinit var rvOrder: RecyclerView
lateinit var orderPresenter: OrderFragmentPresenter
lateinit var adapter: OrderRvAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val orderView = View.inflate(activity, R.layout.fragment_order, null)
orderPresenter = OrderFragmentPresenter(this)
rvOrder = orderView.find<RecyclerView>(R.id.rv_order_list)
rvOrder.layoutManager = LinearLayoutManager(activity)
adapter = OrderRvAdapter(activity)
rvOrder.adapter = adapter
return orderView
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
//访问服务器,获取所有订单数据
val userId = TakeoutApp.sUser.id
if (-1 == userId) {
toast("请先登录,再查看订单")
} else {
orderPresenter.getOrderList(userId.toString())
}
}
fun onOrderSuccess(orderList: List<Order>) {
//TODO:给adapter设置数据
adapter.setOrderData(orderList)
toast("服务器onOrderSuccess")
}
fun onOrderFailed() {
toast("服务器onOrderFailed")
}
}
OrderFragmentPresenter.kt数据的请求和解析
package com.example.takeout.presenter
import com.example.takeout.model.beans.Order
import com.example.takeout.ui.fragment.OrderFragment
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
class OrderFragmentPresenter(val orderFragment: OrderFragment) : NetPresenter() {
fun getOrderList(userId: String) {
val takeoutorderCall = takeoutService.getOrderList()
takeoutorderCall.enqueue(callback)
}
override fun parserJson(json: String) {
//此处解析,List<Order>
val orderList: List<Order> =
Gson().fromJson(json, object : TypeToken<List<Order>>() {}.type)
if (orderList.isNotEmpty()) {
orderFragment.onOrderSuccess(orderList)
} else {
orderFragment.onOrderFailed()
}
}
}
OrderRvAdapter.kt订单界面RecycleView数据的填充
package com.example.takeout.ui.adapter
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.takeout.R
import com.example.takeout.model.beans.Order
import com.mob.wrappers.PaySDKWrapper
import org.jetbrains.anko.find
class OrderRvAdapter (val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var orderList: List<Order> = ArrayList<Order>()
fun setOrderData(orders: List<Order>) {
this.orderList = orders
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
// val itemView = View.inflate(context, R.layout.item_order_item, null)
//TODO:没有填充满,原因是recycleview的孩子,测量模式是UNSPECIFY
//通过返回值已经addview,如果attachToRoot使用true会再一次addView(),就会报错
val itemView = LayoutInflater.from(context).inflate(R.layout.item_order_item,parent, false)
return OrderItemHolder(itemView)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
(holder as OrderItemHolder).bindData(orderList.get(position))
}
override fun getItemCount(): Int {
return orderList.size
}
inner class OrderItemHolder(item: View) : RecyclerView.ViewHolder(item) {
val tvSellerName: TextView
val tvOrderType: TextView
init {
//findviewbyId tv_order_item_seller_name tv_order_item_type
tvSellerName = item.find(R.id.tv_order_item_seller_name)
tvOrderType = item.find(R.id.tv_order_item_type) //订单状态
// item.setOnClickListener {
// val intent: Intent = Intent(context, OrderDetailActivity::class.java)
// intent.putExtra("orderId", order.id)
// intent.putExtra("type", order.type)
// context.startActivity(intent)
// }
}
fun bindData(order: Order) {
// tvSellerName.text = order.seller.name
tvOrderType.text = order.type
}
}
}
TakeoutService.kt使用Retrofit请求数据的封装
package com.example.takeout.model.net
import retrofit2.Call
import retrofit2.http.GET
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>
}
item_order_item.xml订单界面单个item数据的界面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_order_item_seller_logo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="8dp"
android:src="@mipmap/item_kfc" />
<!--<TextView android:gravity="center"-->
<!--android:id="@+id/tvCount"-->
<!--android:textColor="#fff"-->
<!--android:text="1"-->
<!--android:visibility="gone"-->
<!--android:textSize="12sp"-->
<!--android:background="@drawable/circle_red"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_gravity="right"-->
<!--/>-->
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:id="@+id/tv_order_item_seller_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:singleLine="true"
android:text="肯德基宅急送(文化路店)"
android:textColor="#000"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_order_item_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:textSize="14sp"
android:text="订单已完成"
android:textColor="#000"/>
</LinearLayout>
<TextView
android:id="@+id/tv_order_item_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="2016-09-04 18:00"
android:textSize="12sp"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/shape_background_division"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="center_vertical">
<TextView
android:id="@+id/tv_order_item_foods"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="红烧肉等3件商品"
android:textColor="#000"
android:textSize="14sp"/>
<TextView
android:id="@+id/tv_order_item_money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="¥31.00"
android:textColor="#000"
android:textSize="12sp"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/shape_background_division"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
>
<TextView
android:id="@+id/tv_order_item_multi_function"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:textSize="16sp"
android:textColor="#3090E6"
android:background="@drawable/shape_background_stroke_blue"
android:padding="4dp"
android:text="再来一单"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Order.kt订单界面从服务器请求的数据
package com.example.takeout.model.beans
class Order {
var id: String? = null
var type: String? = null
var seller: Seller? = null
var rider: Rider? = null
var goodsInfos: List<GoodsInfo>? = null
var distribution: Distribution? = null
var detail: OrderDetail? = null
inner class Rider {
var id = 0
var name: String? = null
var phone: String? = null // public Location location;
}
inner class OrderDetail {
var username: String? = null
var phone: String? = null
var address: String? = null
var pay: String? = null
var time: String? = null
}
inner class Distribution {
// 配送方式
var type: String? = null
// 配送说明
var des: String? = null
}
}
GoodsInfo.kt订单界面的商品bean文件
package com.example.takeout.model.beans
class GoodsInfo {
var id: Int = 0//商品id
var name: String = ""//商品名称
var icon: String = ""//商品图片
var form: String = ""//组成
var monthSaleNum: Int = 0//月销售量
var isBargainPrice: Boolean = false//特价
var isNew: Boolean = false//是否是新产品
var newPrice: String = ""//新价
var oldPrice: Int = 0//原价
var sellerId: Int = 0
//此商品属于那一个类别id以及类别名称
var typeId: Int = 0
var typeName: String = ""
var count: Int =0
constructor() : super() {}
constructor(sellerId: Int, id: Int, name: String, icon: String, form: String, monthSaleNum: Int, bargainPrice: Boolean,
isNew: Boolean, newPrice: String, oldPrice: Int) : super() {
this.id = id
this.name = name
this.icon = icon
this.form = form
this.monthSaleNum = monthSaleNum
this.isBargainPrice = bargainPrice
this.isNew = isNew
this.newPrice = newPrice
this.oldPrice = oldPrice
this.sellerId = sellerId
}
override fun toString(): String {
return "GoodsInfo [id=$id, name=$name, icon=$icon, form=$form, monthSaleNum="
(+monthSaleNum).toString() + ", bargainPrice=" + isBargainPrice + ", isNew=" + isNew + ", newPrice=" + newPrice +", oldPrice=" + oldPrice + "]"
}
}
封装的数据如下:
{
"code": "0",
"data": "[{\"id\":\"0001\",\"type\":\"20\",\"seller\":{\"id\":1,\"name\":\"小骑士\",\"phone\":\"1300000\"},\"goodInfos\":[{\"id\":0,\"name\":\"土豆烧肉\",\"monthSaleNum\":0,\"barginPrice\":false,\"isNew\":false,\"newPrice\":\"10.00\",\"oldPrice\":0,\"sellerId\":0},{\"id\":0,\"name\":\"香菇炖鸡\",\"monthSaleNum\":0,\"barginPrice\":false,\"isNew\":false,\"newPrice\":\"10.00\",\"oldPrice\":0,\"sellerId\":0}],\"distribution\":{\"type\":\"yes\",\"des\":\"好好吃的米饭啊\"},\"detail\":{\"username\":\"用户123\",\"phone\":\"1301230000\",\"address\":\"宝安区\",\"pay\":\"微信支付\",\"time\":\"2000-9-9 18:00\"}},{\"id\":\"0002\",\"type\":\"20\",\"seller\":{\"id\":1,\"name\":\"汤面骑士\",\"phone\":\"1300000\"},\"goodInfos\":[{\"id\":0,\"name\":\"寿司套餐\",\"monthSaleNum\":0,\"barginPrice\":false,\"isNew\":false,\"newPrice\":\"10.00\",\"oldPrice\":0,\"sellerId\":0},{\"id\":0,\"name\":\"圆路寿司\",\"monthSaleNum\":0,\"barginPrice\":false,\"isNew\":false,\"newPrice\":\"10.00\",\"oldPrice\":0,\"sellerId\":0}],\"distribution\":{\"type\":\"yes\",\"des\":\"这家的寿司很好吃的\"},\"detail\":{\"username\":\"用户123\",\"phone\":\"1301230000\",\"address\":\"南山区\",\"pay\":\"微信支付\",\"time\":\"2000-9-9 18:00\"}}]"
}
请求以后服务端的返回值如下:
2020-10-26 00:18:48.953 8584-8584/com.example.takeout E/home:
[{"id":"0001","type":"20","seller":{"id":1,"name":"小骑士","phone":"1300000"},"goodInfos":
[{"id":0,"name":"土豆烧
肉","monthSaleNum":0,"barginPrice":false,"isNew":false,"newPrice":"10.00","oldPrice":0,"sel
lerId":0},{"id":0,"name":"香菇炖
鸡","monthSaleNum":0,"barginPrice":false,"isNew":false,"newPrice":"10.00","oldPrice":0,"sel
lerId":0}],"distribution":{"type":"yes","des":"好好吃的米饭啊"},"detail":{"username":"用户
123","phone":"1301230000","address":"宝安区","pay":"微信支付","time":"2000-9-9 18:00"}},
{"id":"0002","type":"20","seller":{"id":1,"name":"汤面骑士","phone":"1300000"},"goodInfos":
[{"id":0,"name":"寿司套
餐","monthSaleNum":0,"barginPrice":false,"isNew":false,"newPrice":"10.00","oldPrice":0,"sel
lerId":0},{"id":0,"name":"圆路寿
司","monthSaleNum":0,"barginPrice":false,"isNew":false,"newPrice":"10.00","oldPrice":0,"sel
lerId":0}],"distribution":{"type":"yes","des":"这家的寿司很好吃的"},"detail":{"username":"用
户123","phone":"1301230000","address":"南山区","pay":"微信支付","time":"2000-9-9 18:00"}}]
效果如下: