【发布时间】:2021-07-09 07:20:07
【问题描述】:
我的应用程序中有三个片段Home、Profile 和Detail。在主页片段中,我显示了用户列表。当用户单击列表中的任何项目时,它将打开详细信息片段,并且在详细信息片段中我想显示用户的详细信息。我在我的应用程序中使用喷气背包导航组件。我不知道如何使用导航组件将数据传递给另一个片段。
下面是我的代码:
nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.example.studious.HomeFragment"
android:label="Home"
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_homeFragment_to_detailFragment"
app:destination="@id/detailFragment" />
</fragment>
<fragment
android:id="@+id/profileFragment"
android:name="com.example.studious.ProfileFragment"
android:label="Profile"
tools:layout="@layout/fragment_profile" />
<fragment
android:id="@+id/detailFragment"
android:name="com.example.studious.DetailFragment"
android:label="fragment_detail"
tools:layout="@layout/fragment_detail" />
</navigation>
UserAdapter.kt
class UserAdapter(private val context: Context, private val userList:List<User>): RecyclerView.Adapter<UserAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserAdapter.ViewHolder {
return ViewHolder(UserRowBinding.inflate(LayoutInflater.from(parent.context),parent,false))
}
override fun onBindViewHolder(holder: UserAdapter.ViewHolder, position: Int) {
val model = userList[position]
holder.binding.name.text = model.name
holder.binding.city.text = model.city
holder.binding.email.text = model.email
holder.binding.layout.setOnClickListener {
Toast.makeText(context,model.name,Toast.LENGTH_SHORT).show()
}
}
override fun getItemCount(): Int {
return userList.size
}
class ViewHolder(val binding:UserRowBinding): RecyclerView.ViewHolder(binding.root)
}
有人告诉我如何实现所需的功能。
【问题讨论】:
标签: android kotlin android-fragments android-jetpack-navigation