BusinessActivity.kt判断购物车的钱大到一定程度以后跳转到结算界面
package com.example.takeout.ui.activity
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.takeout.R
import com.example.takeout.model.beans.Seller
import com.example.takeout.ui.adapter.CartRvAdapter
import com.example.takeout.ui.fragment.CommentsFragment
import com.example.takeout.ui.fragment.GoodsFragment
import com.example.takeout.ui.fragment.SellerFragment
import com.example.takeout.utils.PriceFormater
import com.example.takeout.utils.TakeoutApp
import kotlinx.android.synthetic.main.activity_business.*
class BusinessActivity : AppCompatActivity(), View.OnClickListener {
var bottomSheetView: View? = null
lateinit var rvCart: RecyclerView
lateinit var cartAdapter: CartRvAdapter
override fun onClick(v: View?) {
when (v?.id) {
R.id.bottom -> showOrHideCart()
//去结算按钮的点击
R.id.tvSubmit -> {
val intent: Intent = Intent(this, ConfirmOrderActivity::class.java)
startActivity(intent)
}
}
}
/***
* 显示底部购物车的dialog
*/
fun showOrHideCart() {
if (bottomSheetView == null) {
//加载要显示的布局
bottomSheetView = LayoutInflater.from(this)
.inflate(R.layout.cart_list, window.decorView as ViewGroup, false)
rvCart = bottomSheetView!!.findViewById(R.id.rvCart) as RecyclerView
rvCart.layoutManager = LinearLayoutManager(this)
cartAdapter = CartRvAdapter(this)
rvCart.adapter = cartAdapter
val tvClear: TextView = bottomSheetView!!.findViewById(R.id.tvClear) as TextView
tvClear.setOnClickListener {
var builder = AlertDialog.Builder(this)
builder.setTitle("确认都不吃了么?")
builder.setPositiveButton("是,我要减肥", object : DialogInterface.OnClickListener {
override fun onClick(dialog: DialogInterface?, which: Int) {
//开始清空购物车,把购物车中商品的数量重置为0
val goodsFragment: GoodsFragment = fragments.get(0) as GoodsFragment
goodsFragment.goodsFragmentPresenter.clearCart()
cartAdapter.notifyDataSetChanged()
//关闭购物车
showOrHideCart()
//刷新右侧
goodsFragment.goodsAdapter.notifyDataSetChanged()
//清空所有红点
clearRedDot()
goodsFragment.goodsTypeAdapter.notifyDataSetChanged()
//更新下方购物篮
updateCartUi()
//清空缓存
TakeoutApp.sInstance.clearCacheSelectedInfo(seller.id.toInt())
}
})
builder.setNegativeButton("不,我还要吃", object : DialogInterface.OnClickListener {
override fun onClick(dialog: DialogInterface?, which: Int) {
}
})
builder.show()
}
}
//判断BottomSheetLayout内容是否显示
if (bottomSheetLayout.isSheetShowing) {
//关闭内容显示
bottomSheetLayout.dismissSheet()
} else {
//显示BottomSheetLayout里面的内容
val goodsFragment: GoodsFragment = fragments.get(0) as GoodsFragment
val cartList = goodsFragment.goodsFragmentPresenter.getCartList()
cartAdapter.setCart(cartList)
if (cartList.size > 0) {
bottomSheetLayout.showWithSheetView(bottomSheetView)
}
}
}
private fun clearRedDot() {
val goodsFragment: GoodsFragment = fragments.get(0) as GoodsFragment
val goodstypeList = goodsFragment.goodsFragmentPresenter.goodstypeList
for (i in 0 until goodstypeList.size) {
val goodsTypeInfo = goodstypeList.get(i)
goodsTypeInfo.redDotCount = 0
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_business)
processIntent()
//微调底部的导航栏适配
if (checkDeviceHasNavigationBar(this)) {
fl_Container.setPadding(0, 0, 0, 48.dp2px())
}
vp.adapter = BusinessFragmentPagerAdapter()
tabs.setupWithViewPager(vp)
bottom.setOnClickListener(this)
}
var hasSelectInfo = false
lateinit var seller: Seller
private fun processIntent() {
if (intent.hasExtra("hasSelectInfo")) {
hasSelectInfo = intent.getBooleanExtra("hasSelectInfo", false)
seller = intent.getSerializableExtra("seller") as Seller
tvDeliveryFee.text = "另需配送费" + PriceFormater.format(seller.deliveryFee.toFloat())
tvSendPrice.text = PriceFormater.format(seller.sendPrice.toFloat()) + "起送"
}
}
val fragments = listOf<Fragment>(GoodsFragment(), SellerFragment(), CommentsFragment())
val titles = listOf<String>("商品", "商家", "评论")
/**
* 把转化功能添加到Int类中作为扩展函数
*/
fun Int.dp2px(): Int {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
toFloat(), resources.displayMetrics
).toInt()
}
//获取是否存在NavigationBar
fun checkDeviceHasNavigationBar(context: Context): Boolean {
var hasNavigationBar = false
val rs = context.getResources()
val id = rs.getIdentifier("config_showNavigationBar", "bool", "android")
if (id > 0) {
hasNavigationBar = rs.getBoolean(id)
}
try {
val systemPropertiesClass = Class.forName("android.os.SystemProperties")
val m = systemPropertiesClass.getMethod("get", String::class.java)
val navBarOverride = m.invoke(systemPropertiesClass, "qemu.hw.mainkeys") as String
if ("1" == navBarOverride) {
hasNavigationBar = false
} else if ("0" == navBarOverride) {
hasNavigationBar = true
}
} catch (e: Exception) {
}
return hasNavigationBar
}
inner class BusinessFragmentPagerAdapter : FragmentPagerAdapter(supportFragmentManager) {
override fun getPageTitle(position: Int): CharSequence {
return titles.get(position)
}
override fun getItem(position: Int): Fragment {
return fragments.get(position)
}
override fun getCount(): Int {
return titles.size
}
}
/**
* 增加的按钮
*/
fun addImageButton(ib: ImageButton, width: Int, height: Int) {
fl_Container.addView(ib, width, height)
}
fun getCartLocation(): IntArray {
val destLocation = IntArray(2)
imgCart.getLocationInWindow(destLocation)
return destLocation
}
/**
* 更新购物车
*/
fun updateCartUi() {
//更新数量,更新总价
var count = 0
var countPrice = 0.0f
//哪些商品属于购物车?
val goodsFragment: GoodsFragment = fragments.get(0) as GoodsFragment
val cartList = goodsFragment.goodsFragmentPresenter.getCartList()
for (i in 0 until cartList.size) {
val goodsInfo = cartList.get(i)
count += goodsInfo.count
countPrice += goodsInfo.count * goodsInfo.newPrice.toFloat()
}
tvSelectNum.text = count.toString()
if (count > 0) {
tvSelectNum.visibility = View.VISIBLE
} else {
tvSelectNum.visibility = View.GONE
}
tvCountPrice.text = PriceFormater.format(countPrice)
//达到最低配送标准以后展示去结算按钮
if (countPrice >= seller.sendPrice.toFloat()) {
tvSubmit.visibility = View.VISIBLE
tvSendPrice.visibility = View.GONE
} else {
tvSubmit.visibility = View.GONE
tvSendPrice.visibility = View.VISIBLE
}
}
}
CommonUtil.kt判断是否有导航栏的工具类
package com.example.takeout.utils
import android.content.Context
class CommonUtil {
companion object {
//获取是否存在NavigationBar
fun checkDeviceHasNavigationBar(context: Context): Boolean {
var hasNavigationBar = false
val rs = context.getResources()
val id = rs.getIdentifier("config_showNavigationBar", "bool", "android")
if (id > 0) {
hasNavigationBar = rs.getBoolean(id)
}
try {
val systemPropertiesClass = Class.forName("android.os.SystemProperties")
val m = systemPropertiesClass.getMethod("get", String::class.java)
val navBarOverride = m.invoke(systemPropertiesClass, "qemu.hw.mainkeys") as String
if ("1" == navBarOverride) {
hasNavigationBar = false
} else if ("0" == navBarOverride) {
hasNavigationBar = true
}
} catch (e: Exception) {
}
return hasNavigationBar
}
}
}
ConfirmOrderActivity.kt结算的界面
package com.example.takeout.ui.activity
import android.content.Intent
import android.os.Bundle
import android.util.TypedValue
import androidx.appcompat.app.AppCompatActivity
import com.example.takeout.R
import com.example.takeout.model.beans.RecepitAddressBean
import com.example.takeout.utils.CommonUtil
import kotlinx.android.synthetic.main.activity_confirm_order.*
class ConfirmOrderActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_confirm_order)
if (CommonUtil.checkDeviceHasNavigationBar(this)) {
activity_confirm_order.setPadding(0, 0, 0, 48.dp2px())
}
rl_location.setOnClickListener {
// val intent = Intent(this, RecepitAddressActivity::class.java)
// startActivityForResult(intent, 1002)
}
tvSubmit.setOnClickListener {
// val intent = Intent(this, OnlinePaymentActivity::class.java)
// startActivity(intent)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == 200) {
if (data != null) {
val address: RecepitAddressBean =
data.getSerializableExtra("address") as RecepitAddressBean
tv_name.text = address.username
//TODO:其他字段类似赋值
}
}
}
fun Int.dp2px(): Int {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
toFloat(), resources.displayMetrics
).toInt()
}
}
activity_confirm_order.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:background="#fff"
android:id="@+id/activity_confirm_order"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#468ade"
android:paddingBottom="15dp"
android:paddingTop="30dp">
<ImageButton
android:id="@+id/ib_back"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@mipmap/abc_ic_ab_back_mtrl_am_alpha"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:text="确认订单"
android:textColor="#fff"
android:textSize="20sp" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rl_location"
android:layout_width="match_parent" android:layout_height="80dp"
android:background="#fff">
<ImageView
android:id="@+id/iv_location"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginLeft="20dp"
android:background="@mipmap/bk_icon_select_location"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"/>
<LinearLayout
android:layout_toRightOf="@id/iv_location"
android:layout_toLeftOf="@+id/iv_arrow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:textStyle="bold"
android:text="黑马程序员"/>
<TextView
android:id="@+id/tv_sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#fd282626"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_vertical"
android:text="先生"/>
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#fd282626"
android:layout_gravity="center_vertical"
android:text="13787006927,18989898989"
android:maxLines="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_label"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:maxLines="1"
android:padding="3dp"
android:textSize="10sp"
android:background="#fff"
android:gravity="center"
android:text=""
android:textColor="#fff"
android:visibility="gone"
android:layout_marginRight="10dp"
/>
<TextView
android:id="@+id/tv_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#fd9b9999"
android:text="中关村软件园"/>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/iv_arrow"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:background="@mipmap/bk_icon_arrow_small"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="3dp"
android:orientation="horizontal">
<View
android:layout_width="50dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
<View
android:layout_width="40dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
<View
android:layout_width="40dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
<View
android:layout_width="40dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
<View
android:layout_width="40dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
<View
android:layout_width="40dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
<View
android:layout_width="40dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
<View
android:layout_width="40dp"
android:layout_height="3dp"
android:background="@mipmap/order_address_bottom_unit"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="7dp"
android:background="#fdd9d7d7"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<View
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#468ade"/>
<View
android:layout_width="15dp"
android:layout_height="15dp"
android:background="@mipmap/bk_icon_in_time"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="送达时间"
android:layout_marginLeft="10dp"
android:textColor="#000"
android:textSize="18sp"
android:layout_gravity="center_vertical"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<LinearLayout
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:text="尽快送达 | 预计17:55"
android:textColor="#468ade"
android:textSize="12sp"
android:layout_gravity="center_vertical"
/>
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:background="@mipmap/bk_icon_arrow_small"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
<LinearLayout
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:text="蜂鸟专送"
android:textColor="#fff"
android:textSize="12sp"
android:padding="3dp"
android:layout_gravity="center_vertical"
android:background="#468ade"
android:layout_marginRight="4dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="超时秒赔"
android:textColor="#468ade"
android:textSize="12sp"
android:layout_gravity="center_vertical"
/>
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:background="@mipmap/bk_icon_arrow_small"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="7dp"
android:background="#fdd9d7d7"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:text="支付方式"
android:textSize="15sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:text="在线支付"
android:layout_toLeftOf="@id/iv_arrow"
android:textSize="12sp"/>
<ImageView
android:id="@id/iv_arrow"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:background="@mipmap/bk_icon_arrow_small"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fdd9d7d7"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:text="红包"
android:textSize="15sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textColor="#fdd9d7d7"
android:text="无红包可用"
android:enabled="false"
android:layout_toLeftOf="@id/iv_arrow"
android:textSize="12sp"/>
<ImageView
android:id="@id/iv_arrow"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:background="@mipmap/bk_icon_arrow_small"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fdd9d7d7"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:text="商家代金券"
android:textSize="15sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:text="没有商家代金券可用"
android:layout_toLeftOf="@id/iv_arrow"
android:textSize="12sp"/>
<ImageView
android:id="@id/iv_arrow"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:background="@mipmap/bk_icon_arrow_small"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="7dp"
android:background="#fdd9d7d7"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:background="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/tv_seller_name"
android:layout_toRightOf="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="#000"
android:text="田老师红烧肉(海淀东北旺西路店)"
android:maxLines="1"
android:textSize="15sp"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fdd9d7d7"/>
<LinearLayout
android:id="@+id/ll_select_goods"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fdd9d7d7"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:text="配送费"
android:textSize="12sp"/>
<TextView
android:id="@+id/tv_deliveryFee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textColor="#000"
android:layout_alignParentRight="true"
android:text="¥4"
android:textSize="12sp"/>
</RelativeLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#aa000000"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_CountPrice"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:text="待支付¥0"
android:textColor="#fff"
android:textSize="18sp" />
<TextView
android:id="@+id/tvSubmit"
android:layout_width="90dp"
android:layout_height="match_parent"
android:background="#22c222"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:text="提交订单"
android:textColor="#fff"
android:textSize="18sp"
/>
</LinearLayout>
</LinearLayout>
效果如下: