Koltin48.Takeout订单界面点击订单进入订单详情界面(34)

OrderRvAdapter.kt订单界面的条目,点击以后会跳转到订单详情界面

package com.example.takeout.ui.adapter

import android.content.Context
import android.content.Intent
import android.util.Log
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.example.takeout.ui.activity.OrderDetailActivity
import com.example.takeout.utils.OrderObservable
import org.jetbrains.anko.find
import org.json.JSONObject
import java.util.*
import kotlin.collections.ArrayList

class OrderRvAdapter (val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>(),
    Observer {

    init {
        OrderObservable.instance.addObserver(this)  //让观察者和被观察者建立绑定关系
    }

    override fun update(o: Observable?, data: Any?) {
        //观察者的响应
        Log.e("order","观察者收到了消息,消息内容:" + data)
        //更新UI
        val jsonObj : JSONObject = JSONObject(data as String)
        val pushOrderId = jsonObj.getString("orderId")
        val pushType = jsonObj.getString("type")
        var index = -1
        for(i in 0 until orderList.size){
            val order = orderList.get(i)
            if(order.id.equals(pushOrderId)){
                order.type = pushType
                index = i
            }
        }
        if(index !=-1) {
            notifyItemChanged(index) //刷新单个条目,减少UI开销
        }
//        notifyDataSetChanged()
    }

    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
        lateinit var order:Order
        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", "30")
                context.startActivity(intent)
            }
        }

        fun bindData(order: Order) {
            this.order = order
            tvSellerName.text = order.seller?.name ?: "aaaa"
            tvOrderType.text = order.type?.let { getOrderTypeInfo(it) }
        }
    }

    private fun getOrderTypeInfo(type: String): String {
        /**
         * 订单状态
         * 1 未支付 2 已提交订单 3 商家接单  4 配送中,等待送达 5已送达 6 取消的订单
         */
        //            public static final String ORDERTYPE_UNPAYMENT = "10";
        //            public static final String ORDERTYPE_SUBMIT = "20";
        //            public static final String ORDERTYPE_RECEIVEORDER = "30";
        //            public static final String ORDERTYPE_DISTRIBUTION = "40";
        //            public static final String ORDERTYPE_SERVED = "50";
        //            public static final String ORDERTYPE_CANCELLEDORDER = "60";

        var typeInfo = ""
        when (type) {
            OrderObservable.ORDERTYPE_UNPAYMENT -> typeInfo = "未支付"
            OrderObservable.ORDERTYPE_SUBMIT -> typeInfo = "已提交订单"
            OrderObservable.ORDERTYPE_RECEIVEORDER -> typeInfo = "商家接单"
            OrderObservable.ORDERTYPE_DISTRIBUTION -> typeInfo = "配送中"
            OrderObservable.ORDERTYPE_SERVED -> typeInfo = "已送达"
            OrderObservable.ORDERTYPE_CANCELLEDORDER -> typeInfo = "取消的订单"
        }
        return typeInfo
    }
}

OrderDetailActivity.kt订单详情界面,有一个观察者来接受服务器发来的消息,服务端会发送订单或者骑手的状态来更新对应在地图中的位置

package com.example.takeout.ui.activity

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.amap.api.maps2d.AMap
import com.amap.api.maps2d.CameraUpdateFactory
import com.amap.api.maps2d.model.BitmapDescriptorFactory
import com.amap.api.maps2d.model.LatLng
import com.amap.api.maps2d.model.Marker
import com.amap.api.maps2d.model.MarkerOptions
import com.example.takeout.R
import com.example.takeout.utils.OrderObservable
import kotlinx.android.synthetic.main.activity_order_detail.*
import java.util.*
import kotlin.collections.ArrayList

//Observer观察者观察推送的变化
class OrderDetailActivity : AppCompatActivity(), Observer {

    override fun update(o: Observable?, arg: Any?) {
    }

    //    override fun update(o: Observable?, data: Any?) {
    private fun update(orderId: String, types: String, data: Any?) {
//        更新UI
        /*接收到服务端的通知发送来的数据更新UI
        val jsonObj: JSONObject = JSONObject(data as String)
        val pushOrderId = jsonObj.getString("orderId")
        val pushType = jsonObj.getString("type")
         */
        val pushOrderId = orderId
        val pushType = types
        if (orderId.equals(pushOrderId)) {
            type = pushType
        }
        val index = getIndex(type)
        (ll_order_detail_type_point_container.getChildAt(index) as ImageView).setImageResource(R.mipmap.order_time_node_disabled)
        (ll_order_detail_type_container.getChildAt(index) as TextView).setTextColor(Color.BLUE)

        when (type) {
            //骑士接单
            OrderObservable.ORDERTYPE_RECEIVEORDER -> {
                //显示地图
                mMapView.visibility = View.VISIBLE
                //移动地图
                aMap.moveCamera(
                    CameraUpdateFactory.newLatLngZoom(
                        LatLng(
                            22.5757890000,
                            113.9232180000
                        ), 16f
                    )
                )
                //标注买卖家 22.5788300000,113.9216030000
                val sellerMarker = aMap.addMarker(
                    MarkerOptions()
                        .position(LatLng(22.5788300000, 113.9216030000))
                        .icon(BitmapDescriptorFactory.fromResource(R.mipmap.order_seller_icon))
                        .title("兰州拉面").snippet("最美味的面条")
                )
                var imageView = ImageView(this)
                imageView.setImageResource(R.mipmap.order_buyer_icon)
                aMap.moveCamera(
                    CameraUpdateFactory.newLatLngZoom(
                        LatLng(
                            22.5788300000,
                            113.9216030000
                        ), 16f
                    )
                )
                val buyerMarker = aMap.addMarker(
                    MarkerOptions()
                        .position(LatLng(22.5765760000, 113.9237870000))
                        .icon(BitmapDescriptorFactory.fromView(imageView))
                        .title("程序员A").snippet("SVIP买家")
                )
            }
            //配送阶段
            OrderObservable.ORDERTYPE_DISTRIBUTION -> {
                points.add(LatLng(22.5764420000, 113.9208900000))
                //骑士登场
                var imageView = ImageView(this)
                imageView.setImageResource(R.mipmap.order_rider_icon)
                riderMarker = aMap.addMarker(
                    MarkerOptions()
                        .position(LatLng(22.5764420000, 113.9208900000))
                        .icon(BitmapDescriptorFactory.fromView(imageView))
                        .title("我是外卖骑士")
                )
//                        .snippet("我是黑马骑士"))
                riderMarker.showInfoWindow()
            }
            /*更新骑手的位置接单、取餐、送餐
            OrderObservable.ORDERTYPE_DISTRIBUTION_RIDER_GIVE_MEAL,
            OrderObservable.ORDERTYPE_DISTRIBUTION_RIDER_TAKE_MEAL -> {
                if (jsonObj.has("lat")) {
                    val lat = jsonObj.getString("lat")
                    val lng = jsonObj.getString("lng")

                    //移动骑士  22.5739110000,113.9180200000
                    //更新骑手位置就是用新位置重新标记骑手
                    riderMarker.hideInfoWindow()
                    riderMarker.position = LatLng(lat.toDouble(), lng.toDouble())
                    aMap.moveCamera(
                        CameraUpdateFactory.newLatLngZoom(
                            LatLng(
                                lat.toDouble(),
                                lng.toDouble()
                            ), 16f
                        )
                    )
                    //测距功能
                    val distance = AMapUtils.calculateLineDistance(
                        LatLng(lat.toDouble(), lng.toDouble()),
                        LatLng(22.5765760000, 113.9237870000)
                    )
                    riderMarker.title = "距离您还有" + Math.abs(distance) + "米"
                    riderMarker.showInfoWindow()

                    //绘制轨迹
                    points.add(LatLng(lat.toDouble(), lng.toDouble()))
                    val polyline = aMap.addPolyline(
                        PolylineOptions().color(Color.RED).width(3.0f)
                            .add(points.get(points.size - 1), points.get(points.size - 2))
                    )
                }
            }
            */
        }
    }

    var points: ArrayList<LatLng> = ArrayList()
    lateinit var riderMarker: Marker
    lateinit var aMap: AMap
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_order_detail)
        processIntent()
        OrderObservable.instance.addObserver(this)
        mMapView.onCreate(savedInstanceState)// 此方法必须重写
        aMap = mMapView.map
        update(orderId, type, 0)
    }

    override fun onDestroy() {
        super.onDestroy()
        //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图
        mMapView.onDestroy()
    }

    override fun onResume() {
        super.onResume()
        //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图
        mMapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制
        mMapView.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态
        mMapView.onSaveInstanceState(outState)
    }

    lateinit var orderId: String
    lateinit var type: String
    private fun processIntent() {
        if (intent.hasExtra("orderId")) {
            orderId = intent.getStringExtra("orderId")
            type = intent.getStringExtra("type")
            val index = getIndex(type)
            (ll_order_detail_type_point_container.getChildAt(index) as ImageView).setImageResource(R.mipmap.order_time_node_disabled)
            (ll_order_detail_type_container.getChildAt(index) as TextView).setTextColor(Color.BLUE)
        }
    }

    private fun getIndex(type: String): Int {
        var index = -1
        //        String typeInfo = "";
        when (type) {
            OrderObservable.ORDERTYPE_UNPAYMENT -> {
            }
            OrderObservable.ORDERTYPE_SUBMIT ->
                //                typeInfo = "已提交订单";
                index = 0
            OrderObservable.ORDERTYPE_RECEIVEORDER ->
                //                typeInfo = "商家接单";
                index = 1
            OrderObservable.ORDERTYPE_DISTRIBUTION,
            OrderObservable.ORDERTYPE_DISTRIBUTION_RIDER_GIVE_MEAL,
            OrderObservable.ORDERTYPE_DISTRIBUTION_RIDER_TAKE_MEAL,
            OrderObservable.ORDERTYPE_DISTRIBUTION_RIDER_RECEIVE ->
                //                typeInfo = "配送中";
                index = 2
            OrderObservable.ORDERTYPE_SERVED ->
                //                typeInfo = "已送达";
                index = 3
            OrderObservable.ORDERTYPE_CANCELLEDORDER -> {
            }
        }//                typeInfo = "未支付";
        //                typeInfo = "取消的订单";
        return index
    }

}

activity_order_detail.xml订单详情界面的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#3090E6"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="30dp">

            <ImageView
                android:id="@+id/iv_order_detail_back"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="18dp"
                android:src="@mipmap/go_back" />

            <TextView
                android:id="@+id/tv_seller_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="22dp"
                android:layout_toRightOf="@+id/iv_order_detail_back"
                android:text="订单详情界面"
                android:textColor="#FFF"
                android:textSize="18sp" />

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="15dp"
                android:src="@mipmap/order_action_contact" />


        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="6dp"
            android:background="@drawable/shape_background_solid_white"
            android:orientation="vertical">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">


                <ImageView
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_marginBottom="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="20dp"
                    android:src="@mipmap/a" />


                <LinearLayout

                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="16dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:singleLine="true"
                        android:text="商家已接单"
                        android:textColor="@color/colorBase"
                        android:textSize="20sp" />


                    <TextView
                        android:id="@+id/tv_order_detail_time"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="4dp"
                        android:text="2016-09-04 18:00 下单"
                        android:textSize="12sp" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_marginTop="12dp"
                        android:text="准时达(未准时到达可获得赔付)"
                        android:textSize="10sp" />
                </LinearLayout>

            </LinearLayout>
            <!--地图控件-->
            <com.amap.api.maps2d.MapView
                android:id="@+id/mMapView"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:visibility="gone"></com.amap.api.maps2d.MapView>




            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginBottom="4dp"
                android:background="#F4F4F4">

                <LinearLayout
                    android:id="@+id/ll_order_detail_type_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1.0"
                        android:gravity="center_horizontal"
                        android:text="订单已提交"
                        android:textSize="10sp" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1.0"
                        android:gravity="center_horizontal"
                        android:text="商家已接单"
                        android:textSize="10sp" />
                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1.0"
                        android:gravity="center_horizontal"
                        android:text="配送中"
                        android:textSize="10sp" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1.0"

                        android:gravity="center_horizontal"
                        android:text="已送达"
                        android:textSize="10sp" />
                </LinearLayout>
                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="3dp"
                        android:layout_marginTop="5dp"
                        android:layout_marginBottom="5dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_gravity="center_vertical"
                        android:background="@drawable/shape_background_division_order"/>

                    <LinearLayout
                        android:id="@+id/ll_order_detail_type_point_container"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1.0"

                            android:src="@mipmap/order_time_node_normal" />

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1.0"
                            android:src="@mipmap/order_time_node_normal" />

                        <ImageView

                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1.0"
                            android:src="@mipmap/order_time_node_normal" />
                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1.0"
                            android:src="@mipmap/order_time_node_normal" />
                    </LinearLayout>



                </FrameLayout>


            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:src="@mipmap/order_info" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

shape文件

shape_background_solid_white.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

    <!-- 填充颜色 -->
    <solid android:color="#FFF"></solid>

    <!-- 矩形的圆角半径 -->
    <corners android:radius="5dp" />
</shape>

shape_background_division_order.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">

    <!-- 显示虚线,破折线的宽度为dashWith,空隙的宽度为dashGap, darkgray -->
    <stroke android:width="1dp"
        android:color="#DEDEDE"
        android:dashWidth="1dp"
        android:dashGap="3dp" />
    <size android:height="1dp" />
</shape>

效果如下:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值