Koltin43.Takeout首頁詳情介面底部購物欄資料增加減少的處理(29)
BusinessActivity.kt對底部佈局點選的監聽,彈出dialog並用RecycleView填充佈局
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.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 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()
}
})
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)
//微調底部的導航欄適配
if (checkDeviceHasNavigationBar(this)) {
fl_Container.setPadding(0, 0, 0, 48.dp2px())
}
vp.adapter = BusinessFragmentPagerAdapter()
tabs.setupWithViewPager(vp)
bottom.setOnClickListener(this)
}
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)
}
}
GoodsFragmentPresenter.kt清空購物車處理的邏輯
package com.example.takeout.presenter
import android.util.Log
import com.example.takeout.model.beans.GoodsInfo
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()
val allTypeGoodsList: ArrayList<GoodsInfo> = 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 + "個類別商品")
for (i in 0 until goodstypeList.size) {
val goodsTypeInfo = goodstypeList.get(i)
var aTypeCount = 0
val aTypeList: List<GoodsInfo> = goodsTypeInfo.list
for (j in 0 until aTypeList.size) {
val goodsInfo = aTypeList.get(j)
//建立雙向繫結關係
goodsInfo.typeName = goodsTypeInfo.name
goodsInfo.typeId = goodsTypeInfo.id
}
allTypeGoodsList.addAll(goodsTypeInfo.list)
}
goodsFragment.onLoadBusinessSuccess(goodstypeList, allTypeGoodsList)
}
//根據商品類別id找到此類別第一個商品的位置
fun getGoodsPositionByTypeId(typeId: Int): Int {
var position = -1 //-1表示未找到
for(j in 0 until allTypeGoodsList.size){
val goodsInfo = allTypeGoodsList.get(j)
if(goodsInfo.typeId == typeId){
position = j
break;
}
}
return position
}
//根據類別id找到其在左側列表中的position,遍歷左側的列表
fun getTypePositionByTypeId(newTypeId: Int):Int {
var position = -1 //-1表示未找到
for(i in 0 until goodstypeList.size){
val goodsTypeInfo = goodstypeList.get(i)
if(goodsTypeInfo.id == newTypeId){
position = i
break;
}
}
return position
}
fun getCartList() : ArrayList<GoodsInfo> {
val cartList = arrayListOf<GoodsInfo>()
//count >0的為購物車商品
for(j in 0 until allTypeGoodsList.size){
val goodsInfo = allTypeGoodsList.get(j)
if(goodsInfo.count>0){
cartList.add(goodsInfo)
}
}
return cartList
}
fun clearCart() {
val cartList = getCartList()
for(j in 0 until cartList.size) {
val goodsInfo = cartList.get(j)
goodsInfo.count = 0
}
}
}
Constants.kt常量的處理
package com.example.takeout.utils
class Constants {
companion object {
val ADD = 1
val MINUS = -1
}
}
CartRvAdapter.kt底部佈局listview的佈局,RecycleView的處理,有點選事件的處理和小紅點的處理邏輯
package com.example.takeout.ui.adapter
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.takeout.R
import com.example.takeout.model.beans.GoodsInfo
import com.example.takeout.ui.activity.BusinessActivity
import com.example.takeout.ui.fragment.GoodsFragment
import com.example.takeout.utils.PriceFormater
class CartRvAdapter(val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
val goodsFragment: GoodsFragment
init {
goodsFragment = (context as BusinessActivity).fragments.get(0) as GoodsFragment
}
var cartList: ArrayList<GoodsInfo> = arrayListOf()
fun setCart(cartList: ArrayList<GoodsInfo>) {
this.cartList = cartList
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val itemView = LayoutInflater.from(context).inflate(R.layout.item_cart, parent, false)
return CartItemHolder(itemView)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
(holder as CartItemHolder).bindData(cartList.get(position))
}
override fun getItemCount(): Int {
return cartList.size
}
inner class CartItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
View.OnClickListener {
override fun onClick(v: View?) {
var isAdd = false
when (v?.id) {
R.id.ib_add -> {
isAdd = true
doAddOperation()
}
R.id.ib_minus -> doMinusOperation()
}
processRedDotCount(isAdd) //紅點處理
(context as BusinessActivity).updateCartUi() //下方購物欄重新整理
}
private fun processRedDotCount(isAdd: Boolean) {
//找到此商品屬於的類別
val typeId = goodsInfo.typeId
//找到此類別在左側列表中的位置(遍歷)
val typePosition = goodsFragment.goodsFragmentPresenter.getTypePositionByTypeId(typeId)
//最後找出tvRedDotCount
val goodsTypeInfo = goodsFragment.goodsFragmentPresenter.goodstypeList.get(typePosition)
var redDotCount = goodsTypeInfo.redDotCount
if (isAdd) {
redDotCount++
} else {
redDotCount--
}
goodsTypeInfo.redDotCount = redDotCount
//重新整理左側列表
goodsFragment.goodsTypeAdapter.notifyDataSetChanged()
}
private fun doMinusOperation() {
//資料層count
var count = goodsInfo.count
if (count == 1) {
//數量為1再減需要移除此條目
cartList.remove(goodsInfo)
if (cartList.size == 0) {
//最後一個類別移除後,關閉購物車
(context as BusinessActivity).showOrHideCart()
}
//刪除快取
// TakeoutApp.sInstance.deleteCacheSelectedInfo(goodsInfo.id)
} else {
//更新快取
// TakeoutApp.sInstance.updateCacheSelectedInfo(goodsInfo.id, Constants.MINUS)
}
count--
goodsInfo.count = count
//購物車內部數量與價格
notifyDataSetChanged()
//右側列表
goodsFragment.goodsAdapter.notifyDataSetChanged()
}
private fun doAddOperation() {
//資料層count
var count = goodsInfo.count
//更新快取
// TakeoutApp.sInstance.updateCacheSelectedInfo(goodsInfo.id, Constants.ADD)
count++
goodsInfo.count = count
//購物車內部數量與價格
notifyDataSetChanged()
//右側列表
goodsFragment.goodsAdapter.notifyDataSetChanged()
}
val tvName: TextView
val tvAllPrice: TextView
val tvCount: TextView
val ibAdd: ImageButton
val ibMinus: ImageButton
lateinit var goodsInfo: GoodsInfo
init {
tvName = itemView.findViewById(R.id.tv_name) as TextView
tvAllPrice = itemView.findViewById(R.id.tv_type_all_price) as TextView
tvCount = itemView.findViewById(R.id.tv_count) as TextView
ibAdd = itemView.findViewById(R.id.ib_add) as ImageButton
ibMinus = itemView.findViewById(R.id.ib_minus) as ImageButton
ibAdd.setOnClickListener(this)
ibMinus.setOnClickListener(this)
}
fun bindData(goodsInfo: GoodsInfo) {
this.goodsInfo = goodsInfo
tvName.text = goodsInfo.name
tvAllPrice.text = PriceFormater.format(goodsInfo.newPrice.toFloat() * goodsInfo.count)
tvCount.text = goodsInfo.count.toString()
}
}
}
cart_list.xml購物車模組的彈出的佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#d7d4d4">
<ImageView
android:id="@+id/iv"
android:layout_width="5dp"
android:layout_height="20dp"
android:background="#227be1"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/iv"
android:text="購物車"/>
<TextView
android:id="@+id/tvClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:drawableLeft="@mipmap/icon_clean_up"
android:layout_alignParentRight="true"
android:drawablePadding="5dp"
android:layout_marginRight="20dp"
android:text="清空"/>
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvCart"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
item_cart.xml購物車彈出視窗的RecycleView的佈局的條目佈局
<?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="60dp"
android:background="#fff"
android:gravity="center_vertical"
>
<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_weight="1"
android:singleLine="true"
android:layout_height="wrap_content"
android:text="外帶全家桶"
android:textSize="20sp"
android:textColor="#000"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/tv_type_all_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥60"
android:textSize="20sp"
android:textColor="#cd5656"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"/>
<LinearLayout
android:id="@+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:orientation="horizontal">
<ImageButton
android:id="@+id/ib_minus"
android:layout_width="20dp"
android:layout_height="20dp"
android:visibility="visible"
android:background="@mipmap/button_minus"/>
<TextView
android:id="@+id/tv_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="0"
android:textColor="#000"
android:layout_marginLeft="5dp"
android:visibility="visible"
android:layout_gravity="center_vertical"
android:layout_marginRight="5dp"/>
<ImageButton
android:id="@+id/ib_add"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="20dp"
android:background="@mipmap/button_add"/>
</LinearLayout>
</LinearLayout>
效果如下:
相關文章
- laravel欄位減少增加Laravel
- recycleview聊天介面的從底部開始顯示的處理View
- 資料處理之欄位合併
- day83:luffy:新增購物車&導航欄購物車數字顯示&購物車頁面展示
- 蝦皮商品詳情介面返回資料的json格式JSON
- 採集淘寶商品詳情頁資料
- thinkphp5 分頁資料物件的處理PHP物件
- thinkPHP 分頁後如何處理資料PHP
- 京東微信購物首頁效能優化實踐優化
- 使用商品詳情API介面獲取商品資料API
- python批次採集1688商品詳情資料介面+1688商品列表資料介面+1688商品API資料介面PythonAPI
- 1688 API分享:抓取1688商品詳情頁資料API
- 前端頁面優化,減少 reflow 的方法前端優化
- 詳解資料處理的六步驟
- 大量資料如何做分頁處理
- 如何在首頁底部新增公安備案號?
- 淘寶商品詳情資料API介面php java pythonAPIPHPJavaPython
- Temu api介面 獲取商品詳情 資料採集API
- lazada商品詳情資料採集介面程式碼展示
- UCI資料集詳解及其資料處理(附148個資料集及處理程式碼)
- dcat-admin 詳情頁多欄佈局開發心得
- 1688商品詳情資料介面、商品列表介面,商品屬性介面、商品優惠券介面
- 【jquery】實現購物車加減jQuery
- JAVA 通過jsonpath解析懂車帝詳情頁介面JavaJSON
- !!!網頁詳情頁成功!!!網頁
- MySQL資料庫InnoDB壞頁處理修復MySql資料庫
- React 小案例 無線首頁底部導航元件React元件
- 如何使用商品詳情API介面來獲取想要的商品資料?API
- Python資料處理(二):處理 Excel 資料PythonExcel
- 鴻蒙Navigation處理啟動頁跳轉到首頁問題鴻蒙Navigation
- 淘寶拼多多京東上貨必備API 商品詳情頁資料抓取 APP商品詳情原資料APIAPP
- 淘寶商品詳情APP原資料介面:解鎖億級商品資料的秘密!APP
- 自定義 behavior 完美仿 QQ 瀏覽器首頁,美團商家詳情頁瀏覽器
- MySQL實現當前資料表的所有時間都增加或減少指定的時間間隔(推薦)MySql
- 詳解AI開發中的資料預處理(清洗)AI
- 專欄 | 基於 Jupyter 的特徵工程手冊:資料預處理(二)特徵工程
- 專欄 | 基於 Jupyter 的特徵工程手冊:資料預處理(一)特徵工程
- 專欄 | 基於 Jupyter 的特徵工程手冊:資料預處理(四)特徵工程