Android 如何从活动回收器适配器更改活动中按钮的可见性
我想更改“继续卡片”按钮的可见性, 要在用户单击“添加”按钮时可见,并在删除所有项目时返回“不可见”,请执行以下操作: 可见性在布局中设置为不可见 我添加了一个count变量进行比较 RestaurantMenuRecyclerAdapter.ktAndroid 如何从活动回收器适配器更改活动中按钮的可见性,android,android-studio,kotlin,android-recyclerview,Android,Android Studio,Kotlin,Android Recyclerview,我想更改“继续卡片”按钮的可见性, 要在用户单击“添加”按钮时可见,并在删除所有项目时返回“不可见”,请执行以下操作: 可见性在布局中设置为不可见 我添加了一个count变量进行比较 RestaurantMenuRecyclerAdapter.kt package com.himanshu.hungerhunt.adapter import com.himanshu.hungerhunt.R import com.himanshu.hungerhunt.model.FoodMenu impo
package com.himanshu.hungerhunt.adapter
import com.himanshu.hungerhunt.R
import com.himanshu.hungerhunt.model.FoodMenu
import com.himanshu.hungerhunt.databse.FoodEntity
import com.himanshu.hungerhunt.activity.RestaurantMenuActivity
import android.widget.Toast
import android.widget.Button
import android.widget.TextView
import android.view.View
import android.view.LayoutInflater
import android.view.ViewGroup
import android.content.Context
import android.content.SharedPreferences
import androidx.recyclerview.widget.RecyclerView
class RestaurantMenuRecyclerAdapter(val context: Context, private val itemList: ArrayList<FoodMenu>) :
RecyclerView.Adapter<RestaurantMenuRecyclerAdapter.RestaurantMenuViewHolder>() {
var total: Int = 0
var count = 0
class RestaurantMenuViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val txtFoodId: TextView = view.findViewById(R.id.txtFoodId)
val txtFoodName: TextView = view.findViewById(R.id.txtFoodName)
val txtFoodPrice: TextView = view.findViewById(R.id.txtFoodPrice)
val btnAddFood: Button = view.findViewById(R.id.btnAddToCart)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RestaurantMenuViewHolder {
val view =
LayoutInflater.from(parent.context).inflate(R.layout.recycler_food_item, parent, false)
return RestaurantMenuViewHolder(view)
}
override fun getItemCount(): Int {
return itemList.size
}
override fun onBindViewHolder(holder: RestaurantMenuViewHolder, position: Int) {
val foodMenu = itemList[position]
val foodName = foodMenu.foodName
val foodId = foodMenu.foodId
val foodPrice = foodMenu.foodPrice
holder.txtFoodId.text = (position + 1).toString()
holder.txtFoodName.text = foodMenu.foodName
holder.txtFoodPrice.text = foodMenu.foodPrice
val foodEntity = FoodEntity(foodId, foodName, foodPrice)
holder.btnAddFood.setOnClickListener {
val sharedPreferences: SharedPreferences =
context.getSharedPreferences("Cart Preferences", Context.MODE_PRIVATE)
if (!RestaurantMenuActivity.DBAsyncTask(context, foodEntity, 1).execute().get()) {
val async = RestaurantMenuActivity.DBAsyncTask(context, foodEntity, 2).execute()
val result = async.get()
if (result) {
Toast.makeText(context, "${holder.txtFoodName.text} Added", Toast.LENGTH_SHORT)
.show()
holder.btnAddFood.setBackgroundResource(R.color.lightOrange)
holder.btnAddFood.text = "Remove"
total += foodMenu.foodPrice.toInt()
count += 1
sharedPreferences.edit().putInt("total", total).apply()
} else {
Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show()
}
} else {
val async = RestaurantMenuActivity.DBAsyncTask(context, foodEntity, 3).execute()
val result = async.get()
if (result) {
Toast.makeText(
context,
"${holder.txtFoodName.text} Removed",
Toast.LENGTH_SHORT
).show()
holder.btnAddFood.setBackgroundResource(R.color.materialRed)
holder.btnAddFood.text = "Add"
total -= foodMenu.foodPrice.toInt()
count -= 1
sharedPreferences.edit().putInt("total", total).apply()
} else {
Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show()
}
}
}
}
}
包com.himanshu.hungerhunt.adapter
导入com.himanshu.hungerhunt.R
导入com.himanshu.hungerhunt.model.FoodMenu
导入com.himanshu.hungerhunt.database.FoodEntity
导入com.himanshu.hungerhunt.activity.RestaurantMenuActivity
导入android.widget.Toast
导入android.widget.Button
导入android.widget.TextView
导入android.view.view
导入android.view.LayoutInflater
导入android.view.ViewGroup
导入android.content.Context
导入android.content.SharedReferences
导入androidx.recyclerview.widget.recyclerview
类RestaurantMenuRecyclerAdapter(val上下文:上下文,专用val项列表:ArrayList):
RecyclerView.Adapter(){
变量总数:Int=0
变量计数=0
类RestaurantMenuViewHolder(视图:视图):RecyclerView.ViewHolder(视图){
val txtFoodId:TextView=view.findViewById(R.id.txtFoodId)
val txtFoodName:TextView=view.findViewById(R.id.txtFoodName)
val txtFoodPrice:TextView=view.findViewById(R.id.txtFoodPrice)
val btnAddFood:Button=view.findViewById(R.id.btnAddToCart)
}
重写CreateViewHolder(父级:ViewGroup,viewType:Int):RestaurantMenuViewHolder{
瓦尔视图=
LayoutFlater.from(parent.context)。充气(R.layout.recycler\u food\u item,parent,false)
返回餐厅菜单持有者(视图)
}
重写getItemCount():Int{
returnitemlist.size
}
覆盖BindViewHolder(holder:RestaurantMenuViewHolder,位置:Int){
val foodmens=项目列表[位置]
val foodName=foodmens.foodName
val foodId=foodmens.foodId
val foodPrice=foodmens.foodPrice
holder.txtFoodId.text=(位置+1).toString()
holder.txtFoodName.text=foodmens.foodName
holder.txtFoodPrice.text=foodmens.foodPrice
val foodEntity=foodEntity(foodId、foodName、foodPrice)
holder.btnAddFood.setOnClickListener{
val SharedReferences:SharedReferences=
context.getSharedReferences(“购物车首选项”,context.MODE\u PRIVATE)
如果(!RestaurantMenuActivity.DBAsyncTask(上下文,foodEntity,1).execute().get()){
val async=RestaurantMenuActivity.DBAsyncTask(上下文,foodEntity,2).execute()
val result=async.get()
如果(结果){
Toast.makeText(添加了上下文“${holder.txtFoodName.text}”,Toast.LENGTH\u SHORT)
.show()
holder.btnAddFood.setBackgroundResource(R.color.浅橙色)
holder.btnAddFood.text=“删除”
总计+=foodMenu.foodPrice.toInt()
计数+=1
SharedReferences.edit().putInt(“总计”,总计).apply()
}否则{
Toast.makeText(上下文,“错误”,Toast.LENGTH\u SHORT.show())
}
}否则{
val async=RestaurantMenuActivity.DBAsyncTask(上下文,foodEntity,3).execute()
val result=async.get()
如果(结果){
Toast.makeText(
上下文
“${holder.txtFoodName.text}已删除”,
吐司长度
).show()
持有者。btnAddFood。挫折背景资源(R.颜色。材料红色)
holder.btnAddFood.text=“添加”
总计-=foodMenu.foodPrice.toInt()
计数-=1
SharedReferences.edit().putInt(“总计”,总计).apply()
}否则{
Toast.makeText(上下文,“错误”,Toast.LENGTH\u SHORT.show())
}
}
}
}
}
我试过这个
RestaurantMenuActivity().btnprocedToCart.findViewById(R.id.btnprocedToCart).visibility=View.VISIBLE
但它不起作用
您可以使用一个界面,当您单击适配器中的addFood按钮时,该界面将在活动中通知您
class RestaurantMenuRecyclerAdapter(val context: Context, private val itemList:
ArrayList<FoodMenu>) :
RecyclerView.Adapter<RestaurantMenuRecyclerAdapter.RestaurantMenuViewHolder>() {
interface RestaurantMenuListener {
fun onAddClick()
}
}
首先,在适配器中创建接口
class RestaurantMenuRecyclerAdapter(val context: Context, private val itemList:
ArrayList<FoodMenu>) :
RecyclerView.Adapter<RestaurantMenuRecyclerAdapter.RestaurantMenuViewHolder>() {
interface RestaurantMenuListener {
fun onAddClick()
}
}
最后,在适配器中,当您单击“添加”按钮时
holder.btnAddFood.setOnClickListener {
mCallback.onAddClick()
}
您可以使用一个界面,当您单击适配器中的addFood按钮时,该界面将在活动中通知您
class RestaurantMenuRecyclerAdapter(val context: Context, private val itemList:
ArrayList<FoodMenu>) :
RecyclerView.Adapter<RestaurantMenuRecyclerAdapter.RestaurantMenuViewHolder>() {
interface RestaurantMenuListener {
fun onAddClick()
}
}
首先,在适配器中创建接口
class RestaurantMenuRecyclerAdapter(val context: Context, private val itemList:
ArrayList<FoodMenu>) :
RecyclerView.Adapter<RestaurantMenuRecyclerAdapter.RestaurantMenuViewHolder>() {
interface RestaurantMenuListener {
fun onAddClick()
}
}
最后,在适配器中,当您单击“添加”按钮时
holder.btnAddFood.setOnClickListener {
mCallback.onAddClick()
}