https://kotlinlang.org logo
#android
Title
# android
z

ZariApps

08/30/2020, 6:56 PM
i am using navigation, fragments, & recyclerviews
i

Ian Lake

08/30/2020, 8:13 PM
If you're using Navigation, you'd just use
view.findNavController().navigate(R.id.your_id)
- you never reference fragments by class name
z

ZariApps

08/31/2020, 5:37 PM
@Ian Lake - i want each item in the recyclerview to navigate to its respective location. i guess the only problem i have with this is "view.button.setOnClickListener". what do i replace the button with?
Copy code
override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val view: View = inflater.inflate(R.layout.fragment_title, container, false)

    view.Btn.setOnClickListener { view: View ->
        view.findNavController().navigate(R.id.action_titleFragment_to_secondFragment)
    }
    return view
}
i

Ian Lake

08/31/2020, 5:47 PM
You set click listeners for RecyclerView in your adapter / ViewHolder. There you know what position and therefore what data you have for that row
z

ZariApps

08/31/2020, 5:47 PM
ahhhhhhh
ian you are a life saver
let me try that
Copy code
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val currentItem = homeFeed.data[position]
    holder.itemView.setOnClickListener(
    Navigation.createNavigateOnClickListener(R.id.action_secondFragment_to_ListFragment)
    )
@Ian Lake something like this?
i

Ian Lake

08/31/2020, 5:51 PM
Something like that, yeah. I assume your
currentItem
might have fields you want to pass as arguments to your call or would be where you'd store the
R.id
for the destination that row is associated with
10 Views