我創建了一個完美運行的應用程式。我決定對我的應用程式進行一些更改,即使用Fragment而不是Activity在某些地方。此活動中有一個RecyclerView,用于加載從Firestore. 一切都將保持不變,除了Fragment將用于代替Activity.
現在我遇到的問題是recyclerView需要背景關系的配接器。
open class CargoListAdapter(
private val context: Context,
private var list: ArrayList<Cargo>
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
當一個按鈕被點擊時,我必須檢查背景關系以采取進一步的行動。
holder.binding.ibDelete.setOnClickListener {
when (context) {
is OrderActivity -> {
val activity = holder.itemView.context as? OrderActivity
activity?.viewSpecialRequest(
model.custom_request
)
}
is MyOrderDetailsActivity -> {
val activity = holder.itemView.context as? MyOrderDetailsActivity
activity?.viewSpecialRequest(
model.custom_request
)
}
is CargoFragment -> {
val activity = holder.itemView.context as? CargoFragment
activity?.editDeleteSpecialRequestDialog(
holder.binding.ibEditDeleteNote,
holder.binding.ibAddNote,
model.id,
model.custom_request
)
}
}
}
這就是我從其中一項活動中呼叫的方式。
val cargoListAdapter = CargoListAdapter(this@OrderActivity, mCargoList)
我試圖從fragment下面的類似呼叫,但我可以看到一個錯誤“不兼容的型別:CargoFragment 和背景關系”在is CargoFragment -> {
val cargoListAdapter = CargoListAdapter(requireContext(), mCargoList)
EDIT:
This is not part of the question but to show what I have done when there are a couple of buttons in the recyclerView and take action depending on which button is clicked. Someone has already asked about it in the accepted answer's comment box.
Now the onClickItem of the adapter class is changed as per my requirement. Where, as you can see, I have action:String and which will let me know which button is clicked and what function/action needs to be done further.
open class CargoListAdapter(
private val context: Context,
private var list: ArrayList<Cargo>,
private val onClickItem: (pos: Int,addButton: ImageButton,deleteButton: ImageButton,model: Cargo,action:String) -> Unit
) : RecyclerView.Adapter<RecyclerView.ViewHolder>()
In the onBindViewHolder I have the following.
holder.binding.ibRemoveItem.setOnClickListener {
onClickItem(holder.adapterPosition,
holder.binding.ibAddNote,
holder.binding.ibDeleteNote,
model,
"removeItem")
}
Whenever a button is clicked a button onClickListener is set up as above and depending on the button I change the value for 'action'. As you can see, in the above example, it's 'removeItem' and hence from the below code the action that comes under 'removeItem' will take place.
Following is what I have in the 'CargoFragment'
when (action) {
"DeleteNote" -> {
///TAKE SOME ACTION HERE
}
"AddNote" -> {
///TAKE SOME ACTION HERE
}
"removeItem" -> {
///TAKE SOME ACTION HERE
}
This is what I have done to handle the situation where there are multiple buttons in the recyclerView and it's working without any flaws. I hope some experts will comment on this so that beginners can know whether this is OK to follow.
uj5u.com熱心網友回復:
您可以使用 Kotlin 高階函式使配接器獨立并在代碼中的任何位置使用,而不是使用介面和使用 Activity 或 Fragment 實體訪問函式。
open class CargoListAdapter(
private val context: Context,
private var list: ArrayList<Cargo>,
private val onClickItem:(pos: Int, viewId: Int) -> Unit
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
單擊時在 ViewHolder 中呼叫 onClickItem :
holder.binding.ibDelete.setOnClickListener {
onClickItem(adapterPosition, it.id)
}
holder.binding.ibEdit.setOnClickListener {
onClickItem(adapterPosition, it.id)
}
在 Activity 或 Fragment 中:
adapter = CargoListAdapter(context, list){position, viewId->
//receive click here
when(viewId){
R.id.ibDelete -> //Delete Action
R.id.ibEdit -> //Delete Action
// You can use sam click listener for multiple views by passing id
}
}
查看此要點以獲得完整的配接器示例:https : //github.com/Noddy20/Gists/blob/master/RecyclerViewAdapter-1.kt
uj5u.com熱心網友回復:
您檢查背景關系,然后將其轉換為創建配接器的活動或片段,在呼叫活動/片段方法后viewSpecialRequest這不是很正確,如果要在單擊后呼叫活動/片段方法,則ibDelete需要添加例如一個界面。
open class CargoListAdapter(
private val context: Context,
private var list: ArrayList<Cargo>
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
interface OnItemActionsListener {
fun onDeleteClicked()
}
var onItemActionsListener:OnItemActionsListener? =null
holder.binding.ibDelete.setOnClickListener {
onItemActionsListener?.onDeleteClicked()
}
*********
}
}
訂單活動
adapter = CargoListAdapter(this, arrayListOf())
adapter.onItemActionsListener = object : CargoListAdapter.OnItemActionsListener {
override fun onDeleteClicked() {
// TODO logic
}
}
貨物碎片
adapter = CargoListAdapter(requireActivity(), arrayListOf())
adapter.onItemActionsListener = object : CargoListAdapter.OnItemActionsListener {
override fun onDeleteClicked() {
// TODO logic
}
}
如果您需要在配接器中執行邏輯,而不是背景關系檢查,您可以使用 enum。
public enum DeleteCaseEnum {
NONE, CASE_1, CASE_2, CASE_3
}
open class CargoListAdapter(
private val context: Context,
private var list: ArrayList<Cargo>
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var deleteCase:DeleteCaseEnum = DeleteCaseEnum .NONE
holder.binding.ibDelete.setOnClickListener {
when (deleteCase) {
is DeleteCaseEnum.CASE_1-> {
// TODO
}
is DeleteCaseEnum.CASE_2-> {
// TODO
}
}
訂單活動
adapter = CargoListAdapter(this, arrayListOf())
adapter.deleteCase = DeleteCaseEnum.CASE_1
貨物碎片
adapter = CargoListAdapter(requireActivity(), arrayListOf())
adapter.deleteCase = DeleteCaseEnum.CASE_2
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/357962.html
標籤:android kotlin android-fragments android-recyclerview
