当在回收器视图中单击时,我试图将数据从适配器传递到片段。我已经实现了点击监听器,但无法将数据传递给片段,也无法在fragmentDetail中获取传递的数据。我是android新手,请帮助我? DSAdapter.kt
class DSAdapter(
private val DSModelList: ArrayList<DSModel>,
private val listener: RecyclerViewEvent
): RecyclerView.Adapter<DSAdapter.ViewHolder>() {
private lateinit var context: Context;
interface RecyclerViewEvent {
fun onItemClick(position: Int)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DSAdapter.ViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.row_algorithm, parent, false)
context = parent.context
return ViewHolder(itemView)
}
override fun onBindViewHolder(holder: DSAdapter.ViewHolder, position: Int) {
val algorithModel = DSModelList.get(position)
var algorithmName = algorithModel.name
var index = algorithModel.index
holder.algorithm_name.text = algorithmName
holder.index.text = index
}
override fun getItemCount(): Int {
return DSModelList.size
}
inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView),View.OnClickListener {
var algorithm_name = itemView.findViewById<TextView>(R.id.algorithm_name)
var index = itemView.findViewById<TextView>(R.id.rvvPrefix)
init {
itemView.setOnClickListener(this)
}
override fun onClick(view: View?) {
val position = adapterPosition
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position)
}
}
}
}
FragmentAlgorithmDetail.kt
class FragmentAlgorithmDetail : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.algorithm_detail, container, false)
}
}
FragmentDSList.kt
class FragmentDSList : Fragment() {
private lateinit var adapter: DSAdapter
private lateinit var recyclerView: RecyclerView
private lateinit var DSModelArrayList: ArrayList<DSModel>
lateinit var algorithmName: Array<String>
lateinit var algorithmIndex: Array<String>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.algorithm_list, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
dataInitialize()
val layoutManager = LinearLayoutManager(context)
recyclerView = view.findViewById(R.id.recycler_view)
recyclerView.layoutManager = layoutManager
recyclerView.setHasFixedSize(true)
adapter = DSAdapter(DSModelArrayList,object: DSAdapter.RecyclerViewEvent {
override fun onItemClick(position: Int) {
val navcontroller = findNavController()
navcontroller.navigate(R.id.action_data_structure_list_to_fragment_AlgorithmDetail)
}
})
recyclerView.adapter = adapter
}
private fun dataInitialize(){
DSModelArrayList = arrayListOf<DSModel>()
algorithmName = arrayOf(
getString(R.string.linked_lists),
getString(R.string.Stacks),
getString(R.string.Queues),
)
algorithmIndex= arrayOf("0","1","2")
for (i in algorithmName.indices){
val index = algorithmIndex[i].toInt() + 1
val algorithmodel = DSModel(index.toString(),algorithmName[i])
DSModelArrayList.add(algorithmodel)
}
}
}
我尝试在回收器视图中单击项目时传递数据并将该数据传递到片段,我期望的是单击项目时将数据传递到片段
您可以使用导航组件在片段之间传递数据:
https://developer.android.com/guide/navigation/use-graph/pass-data
您可以定义在导航 XML 中传递的数据类型,例如:
<fragment android:id="@+id/myFragment" >
<argument
android:name="myArg"
app:argType="integer"
android:defaultValue="0" />
</fragment>
SafeArgs 插件让这个奇迹发生:
https://developer.android.com/guide/navigation/use-graph/safe-args
你已经上了一堂很好的
DSModel
课了。 如果您可以将其设为 Parcelable
类,则可以将其用作参数:
<fragment android:id="@+id/fragmentAlgorithmDetail" >
<argument
android:name="algorithm"
app:argType="com.example.DSModel" />
</fragment>
然后,如果您使用导航类而不是 id,您可以这样说:
val model = DSModelArrayList[position]
navcontroller.navigate(action_DataStructureList_to_AlgorithmDetail(model))
查找有关使用 SafeArgs 进行导航的教程来帮助您理解。