-1

I have three fragments in my app Home, Profile and Detail.In Home fragment I am showing a list of users. When user clicks on any item in the list it will open detail fragment and in the detail fragment I want to show user's details. I am using jetpack navigation component in my app. I have no idea of how I can pass data using navigation component to another fragment.

Below is my code:

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)

}

Someone let me know how can I achieve the desired functionality.

Rahul Rawat
  • 360
  • 2
  • 9
Digvijay
  • 2,887
  • 3
  • 36
  • 86

1 Answers1

-1

Try this developer's documentation.

Edit: If you know about this than I assume you can easily call the action from inside your fragment. So try this create an interface in the adapter file like:-

interface UserCallback {
   //using position in case you need it
   fun userClicked(user: User, position: Int)
}

Then add an interface reference variable to adapter's constructor like

class UserAdapter(private val context: Context, private val userList:List<User>, callback: UserCallback)

and then call callback.userClicked(user, position) in your onClickListener.

After that you just need to provide the definition of this callback in fragment and just call the action from inside that definition.

Also instead of callback you can use lambdas too.

Rahul Rawat
  • 360
  • 2
  • 9