在我的程式中,我有一個帶有配接器的 RecyclerView,我在其中檢查單擊了 RecyclerView 的哪個元素。
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val currentBreakfast = breakfastList[position]
holder.breakfastTitle.text = context.getText(currentBreakfast.breakfastStringResourceId)
holder.breakfastImage.setImageResource(currentBreakfast.breakfastImageResourceId)
holder.breakfastImage.setOnClickListener {
holder.itemView.findNavController().navigate(R.id.action_breakfastFragment_to_DetailsFragment)
showDetails(currentBreakfast)
}
}
我想將有關特定單擊元素的資料(例如imageId、stringId、Name等)傳遞給另一個片段DetailsFragment我想在其中顯示更多資料
我該怎么做?
uj5u.com熱心網友回復:
您需要將片段發送到您呼叫的配接器,我猜它是早餐片段,只需添加this:
BreakfastAdapter(
requireActivity(),
breakfastList,
this
)
你的配接器得到了fragment:
open class BreakfastAdapter(
private val context: Context,
private var breakfastList: ArrayList<Breakfast>,
private val fragment: Fragment
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
您可以使用它fragment來導航:
holder.breakfastImage.setOnClickListener {
// create a bundle to hold data you want to send
val bundle = bundleOf(
"breakfastImageResourceId" to currentBreakfast.breakfastImageResourceId,
"breakfastStringResourceId" to currentBreakfast.breakfastStringResourceId,
"kcal" to currentBreakfast.kcal
)
// add this bundle when you move to another fragment.
fragment.findNavController().navigate(R.id.action_breakfastFragment_to_DetailsFragment, bundle)
}
在另一個片段中獲取該值并在日志中查看它們,如下所示:
// create below variables
private var mBreakfastImageResourceId: Int = 0
private var mBreakfastStringResourceId: Int = 0
private var mKcal: Int = 0
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Now we set above values with what we sent from adapter
arguments?.let {
if (it["breakfastImageResourceId"] != null) {
mBreakfastImageResourceId = it.getInt("BreakfastImageResourceId")!!
}
if (it["breakfastStringResourceId"] != null) {
mBreakfastStringResourceId = it.getInt("breakfastStringResourceId")!!
}
if (it["kcal"] != null) {
mKcal = it.getInt("kcal")!!
}
}
return inflater.inflate(R.layout.fragment_question_stats, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// now you can do whatever you want with mBreakfastImageResourceId, mBreakfastStringResourceId and mKcal.
}
PS我只是編了一些變數和類名,因為你沒有分享它們,只是用你的正確名稱來改變它。
uj5u.com熱心網友回復:
有幾種方法。
我建議您看一下這個答案(已接受的答案): 如何將資料從配接器傳遞到片段?
然后轉到名為“一種更 Kotlin 的方法是忽略介面并只傳遞一個函式”的部分。
這也是我自己使用的方法。
這是我的 RecyclerView 配接器如何定義的示例:
class ReportAdapter(var reports: List<Report>,val clickFunc : (Report) -> (Unit)) : RecyclerView.Adapter<ReportAdapter.ViewHolder>() {...}
然后,您需要將回呼函式附加到您的查看器內的點擊偵聽器 - 在我的示例中,它看起來像這樣(串列中的每個專案都有一個卡片視圖 - 在 xml 檔案中定義):
itemView.report_cardview_item.setOnClickListener {
clickFunc(report)
}
然后在傳遞給 RecylerView Adapter 的回呼函式中,您可以導航到包含相關資料的詳細資訊片段 - 我的回呼函式之一(在創建時傳遞給配接器)看起來像這樣(在“擁有”的片段中定義“回收站視圖):
private fun reportClicked(report:Report)
{
val action = ReportsFragmentDirections.actionNavReportsToReportDetailFragment(report)
findNavController().navigate(action)
}
(這里使用我的導航圖創建用于 NavController 的操作)
我的配接器很容易用一些資料和對回呼函式的參考進行初始化:
adapter = ReportAdapter(reports,::reportClicked)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/532726.html
標籤:安卓科特林安卓片段android-recyclerview
