gpt4 book ai didi

android - Kotlin:如何使用 View 模型/接口(interface)将 fragment 中的数据从 recyclerview 传递到另一个 fragment ?

转载 作者:行者123 更新时间:2023-12-04 23:54:21 25 4
gpt4 key购买 nike

我找不到使用 viewmodelkotlin 中的接口(interface)将数据从 recyclerview 适配器传递到 fragment 的示例。我试图只使用一个包,但我在接收 fragment 中收到一个空包。这是我的Recyclerview 适配器 代码 fragment 。

class QnAMyProfileEditRecyclerViewAdapter(private val context: Context) : RecyclerView.Adapter<QnAMyProfileEditRecyclerViewAdapter.Holder>() {


private var currentUserQnADataMyProfileEdit = emptyList<QnAData>()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {

val view: View = if (viewType == R.layout.rv_item_qn_a_my_profile) {

LayoutInflater.from(parent.context)
.inflate(R.layout.rv_item_qn_a_my_profile, parent, false)

} else {

LayoutInflater.from(parent.context)
.inflate(R.layout.rv_footer_new_qna_my_profile, parent, false)

}
return Holder(view)
}

override fun getItemCount(): Int {
return currentUserQnADataMyProfileEdit.size + 1
}

//implement Floating Action Button at the bottom of the recyclerview
override fun getItemViewType(position: Int): Int {

return if (position == currentUserQnADataMyProfileEdit.size) {
R.layout.rv_footer_new_qna_my_profile
} else {
R.layout.rv_item_qn_a_my_profile
}
}

override fun onBindViewHolder(holder: Holder, position: Int) {

if (position == currentUserQnADataMyProfileEdit.size) {
holder.viewHolderNewQnA?.setOnClickListener {
Toast.makeText(context, "New item intent", Toast.LENGTH_SHORT).show()
}
} else {
//Normal bind for RecyclerView
holder.bindData(currentUserQnADataMyProfileEdit[position])
holder.viewHolderCRUDQnAMyProfile?.setOnClickListener {


//Give QnACrud current QnAData --- send bundle
val bundle = Bundle()
bundle.putString(
"currentQnAQuestion",
currentUserQnADataMyProfileEdit[position].question
)
bundle.putString(
"currentQnAAnswer",
currentUserQnADataMyProfileEdit[position].answer
)
QnAMyProfileEditCRUDFragment().arguments = bundle
val activity: AppCompatActivity = context as AppCompatActivity
activity.supportFragmentManager.beginTransaction()
.add(R.id.cl_my_profile_edit, QnAMyProfileEditCRUDFragment())
.addToBackStack(null).commit()




}
}
}


fun setData(updateList: List<QnAData>) {
this.currentUserQnADataMyProfileEdit = updateList
notifyDataSetChanged()
}

inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {




val viewHolderQuestion =
itemView.findViewById(R.id.txt_question_qna_my_profile_edit) as TextView?
val viewHolderAnswer =
itemView.findViewById(R.id.txt_answer_qna_my_profile_edit) as TextView?

val viewHolderNewQnA =
itemView.findViewById(R.id.fab_new_qna_my_profile_edit) as FloatingActionButton?

val viewHolderCRUDQnAMyProfile =
itemView.findViewById(R.id.fab_crud_qna_my_profile_edit) as FloatingActionButton?


//Bind data with inflaters
fun bindData(currentUserInList: QnAData) {

viewHolderQuestion?.text = currentUserInList.question
viewHolderAnswer?.text = currentUserInList.answer
}

}
}

这是我的 fragment ,它应该接收数据(bundle):

class QnAMyProfileEditCRUDFragment : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

//Get bundle data and set Edit Text values


val bundle:Bundle? = arguments //Bundle == null!!!!
val qnaQuestionData: String? = bundle?.getString("currentQnAQuestion")
val qnaAnswerData: String? = bundle?.getString("currentQnAAnswer")
et_qna_question.setText(qnaQuestionData)
et_qna_answer.setText(qnaAnswerData)


}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {



return inflater.inflate(R.layout.fragment_qn_a_my_profile_edit_c_r_u_d, container, false)
}

override fun onAttach(context: Context) {
super.onAttach(context)


}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

//Pop fragment if grey area(outside the box) is tapped
imv_overlay.setOnClickListener {
fragmentManager?.popBackStack()
}
}

}

QnAData是一个简单的数据类,有两个成员questionanswer都是字符串类型

最佳答案

首先,需要对您的代码进行一些重构:

  1. 内部类可能导致内存泄漏,因此将 ViewHolder 更改为:
  2. 将您的点击监听器放入您的 ViewHolder 中

ViewHolder

class MyViewHolder(...) : RecyclerView.ViewHolder(...) {

fun bind(model: Model) {
...

itemView.setOnClickListener {
callback?.onSomeEventHappened(model)
//you can also pass lambda here instead of using an interface for callback
}

}

}
  1. RecyclerViewAdapter 需要定义一个协议(protocol)来与 fragment 通信:
class Adapter : RecyclerView.Adapter<T>() {

interface callback {
fun onSomeEventHappened()
}


private var callback: callback? = null


fun setListener(listener: Callback) {
callback = listener
}

}
  1. 如果数据在两个 fragment 之间共享,请使用 SharedViewModel
  2. 如果您需要传递数据,您可以使用 AndroidNavigationCompoent 参数或在创建 fragment 时传递它们:
val SomeFragment = SomeFragment().apply {
SomeFragment.arguments = ... //A bundle for example
}

关于android - Kotlin:如何使用 View 模型/接口(interface)将 fragment 中的数据从 recyclerview 传递到另一个 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62499608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com