在我的主要活动中,我有一个RecyclerView。可以添加多种类型的视图。都有不同类型的数据。一个视图类型携带&显示客户信息,而另一个视图类型显示带有附加文本和价格的食品项目。
在用户完成主活动并转移到另一个活动之后,我想传递RecyclerView中的所有视图和数据,并在另一个活动中重新创建它。
我如何封装已输入到RecyclerView中的所有视图,以便它们在活动之间来回传递?它还需要能够处理不同的视图类型与不同的数据。
MainActivity.kt --这里是带有多个视图类型的RecyclerView,我想封装它,以便将它发送到不同的活动并重新打开.
package com.example.app
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.multiplerecyclerview.Adapter
import com.example.app.*
import com.example.app.Activites.*
import com.example.app.model.DataModel
import kotlinx.android.synthetic.main.main_activity.*
const val INDEX = 0
class OrderActivity : AppCompatActivity() {
val list = ArrayList<DataModel>() /*ArrayList that is type Data Model. */
val adapter = Adapter(this, getItemsList()) /* Adapter class is initialized and list is passed in the param. */
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
recyclerView.layoutManager = LinearLayoutManager(this) /* Set the LayoutManager that this RecyclerView will use. */
recyclerView.adapter = orderAdapter /* Adapter instance is set to the recyclerview to inflate the items. */
/* Complete Button is clicked, now encapsulating RecyclerView and passing to next Activity. */
complete_btn.setOnClickListener {
val total = textViewPrice.text.toString()
val intent = Intent(this@MainActivity, LiveOrderActivity::class.java)
intent.putExtra("total", total)
intent.putExtra("customerName", customerName)
intent.putExtra("customerNumber", customerNumber)
intent.putExtra("customerPostal", customerPostal)
intent.putExtra("customerAddress", customerAddress)
/* HOW DO I PASS ALL THE DATA FROM THE RECYCLER VIEW HERE??? */
startActivity(intent)
}
}
/* One ViewType, customer with a certain criteria of data. */
private fun insertCustomer(customerName: String, customerNumber: String, customerPostal: String, customerAddress: String) {
val newItem = DataModel(
customerName = customerName,
customerNumber = customerNumber,
customerPostal = customerPostal,
customerAddress = customerAddress,
viewType = OrderAdapter.CUSTOMER
) /* Adding the item with correct arguments */
list.add(INDEX, newItem) /* Adding Item at Position Index. */
orderAdapter.notifyItemInserted(INDEX) /* Notifying the Adapter of the addition. */
}
/* Another ViewType, with a different set of data. */
private fun insertCharge(charge: String) {
Toast.makeText(this, "Delivery Charge Added!", Toast.LENGTH_SHORT).show() /* Toast Message to confirm insertion. */
val newItem = DataModel(
delivery = "$charge",
viewType = OrderAdapter.DELIVERY_CHARGE
) /* Adding the item with correct arguments */
list.add(INDEX, newItem) /* Adding Item at Position Index. */
orderAdapter.notifyItemInserted(INDEX) /* Notifying the Adapter of the addition. */
}
/* Another ViewType, with a different set of data. */
private fun insertOpenFood(openFoodDetails: String, openFoodPrice: String) {
Toast.makeText(this, "Open Food Added!", Toast.LENGTH_SHORT).show() /* Toast Message to confirm insertion. */
val newItem = DataModel(
openFoodDetails = "$openFoodDetails",
openFoodCharge = "$openFoodPrice",
viewType = OrderAdapter.OPEN_FOOD_CHARGE
) /* Adding the item with correct arguments */
list.add(INDEX, newItem) /* Adding Item at Position Index. */
orderAdapter.notifyItemInserted(INDEX) /* Notifying the Adapter of the addition. */
}
private fun getItemsList(): ArrayList<DataModel> {
//list.add(DataModel("Ham Burger","1","12.50", viewType = OrderAdapter.NO_TOPPING))
//list.add(DataModel("American","1","12.50", viewType = OrderAdapter.NO_TOPPING))
return list
}
}
Adapter.kt --这是我为RecyclerView in MainActivity.kt使用的适配器
package com.example.multiplerecyclerview
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView
import com.example.app.model.DataModel
import com.example.app.R
import kotlinx.android.synthetic.main.openfood_charge.view.*
import kotlinx.android.synthetic.main.delivery_charge.view.*
import kotlinx.android.synthetic.main.customer_item.view.*
class Adapter(val context: Context, val items: ArrayList<DataModel>) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
companion object {
const val NO_TOPPING = 1
const val DELIVERY_CHARGE = 9
const val OPEN_FOOD_CHARGE = 12
const val CUSTOMER = 21
}
/* Depending on the View Type, the correct one is passed. */
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
if(viewType == NO_TOPPING)
return ViewHolder(LayoutInflater.from(context).inflate( R.layout.food_item,parent,false))
else if (viewType == DELIVERY_CHARGE)
return ViewHolder2(LayoutInflater.from(context).inflate(R.layout.delivery_charge,parent,false))
else if (viewType == OPEN_FOOD_CHARGE)
return ViewHolder3(LayoutInflater.from(context).inflate(R.layout.openfood_charge,parent,false))
else if (viewType == CUSTOMER)
return ViewHolder7(LayoutInflater.from(context).inflate(R.layout.customer_item,parent,false))
else
return ViewHolder(LayoutInflater.from(context).inflate( R.layout.food_item,parent,false))
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val item = items.get(position)
if(holder is ViewHolder) {
holder.productQuantity.text = item.itemQuantity
holder.productName.text = item.itemName
holder.productPrice.text = item.itemPrice
}else if(holder is ViewHolder2) {
holder.deliveryPrice.text = item.delivery
} else if(holder is ViewHolder3) {
holder.openFoodDetails.text = item.openFoodDetails
holder.openFoodPrice.text = item.openFoodCharge
} else if(holder is ViewHolder7) {
holder.customer.text = item.customerName
holder.number.text = item.customerNumber
holder.eircode.text = item.customerPostal
holder.address.text = item.customerAddress
}
}
override fun getItemViewType(position: Int): Int {
return items[position].viewType
}
override fun getItemCount(): Int {
return items.size
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view), View.OnClickListener {
val productQuantity = view.productQuantityView
val productName = view.productNameView
val productPrice = view.productPriceView
init { /* Acts like a constructor in Java. */
view.setOnClickListener(this)
}
override fun onClick(v: View?) {
TODO("Not yet implemented")
}
}
interface onItemClickListener {
fun onItemClick()
}
class ViewHolder2(view: View) : RecyclerView.ViewHolder(view), View.OnClickListener {
var deliveryPrice = view.deliveryPriceView
val intent: Intent? = null
init {
view.setOnClickListener(this)
}
override fun onClick(v: View?) {
}
}
class ViewHolder3(view: View) : RecyclerView.ViewHolder(view) {
var openFoodDetails = view.openFoodView
var openFoodPrice = view.openFoodPriceView
}
class ViewHolder7(view: View) : RecyclerView.ViewHolder(view) {
val customer = view.nameView
val number = view.mobileView
val eircode = view.eircodeView
val address = view.address1View
}
}
SecondActivity.kt -在这里,我想接收回收视图的数据并重新创建它。
package com.example.app
class ActiveOrderActivity : AppCompatActivity() {
private val aorderList = generateList(50)
private val adapter = AOrdersAdapter(aorderList)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_active_order)
recyclerview.adapter = adapter
recyclerview.layoutManager = LinearLayoutManager(this)
recyclerview.setHasFixedSize(true)
insertItem()
/* Here I want to recieve the RecyclerView Data! */
val customerName: String = intent.getStringExtra("customerName").toString()
val customerNumber: String = intent.getStringExtra("customerNumber").toString()
if (customerName == "null") { /*If no RecyclerView has been passed. */
} else {
insertNewOrder(customerName, customerNumber, customerPostal, customerAddress, paymentType, paymentTotal)
}
}
fun insertItem() {
}
private fun insertNewOrder(customerName: String, customerNumber: String, customerPostal: String, customerAddress: String, paymentType: String, paymentTotal: String) {
val newItem = ActiveOrderModel(customerName,customerNumber, customerPostal, customerAddress, paymentTotal, paymentType)
aorderList.add(0, newItem) /* Adding Item at Position Index. */
adapter.notifyItemInserted(0) /* Notifying the Adapter of the addition. */
/* Creating a new Customer. */
val order = hashMapOf(
"customer Name" to customerName,
"customer Number" to customerNumber,
"eircode" to customerPostal,
"address" to customerAddress,
"paymentTotal" to paymentTotal
)
}
private fun generateList(size: Int): ArrayList<ActiveOrderModel> {
val list = ArrayList<ActiveOrderModel>()
return list
}
}
发布于 2021-03-16 09:47:04
创建一个接口
interface ViewHolderCallback{ fun onViewHolderSumitData(val data: Data) }
然后,您的ViewHolder、ViewHolder2、ViewHolder3、ViewHolder7将需要通过其构造函数实现该ViewHolderCallback接口。
class ViewHolder(view: View, val callback: ViewHolderCallback)
您的活动将实现ViewHolderCallback,这需要它覆盖onViewHolderSumitData方法。您将在这里收到Viewholder的数据。想做什么就做什么。
然后,在您的视图持有者中,当您觉得需要发送数据到活动执行callback.onViewHolderSumitData时,您将需要根据ViewHolder中的数据创建数据解析器。
如果onViewHolderSumitData中的数据参数给您带来困难的话。只需创建方法no即可。让活动实现它,然后设置调试断点。你会明白的。
P/s您可以从活动到查看适配器的界面实现。
class Adapter(callback: ViewHolderCallback)
https://stackoverflow.com/questions/66651503
复制相似问题