Answer the question
In order to leave comments, you need to log in
How can I make it open another fragment when clicking on a RecyclerView item?
I want to make it so that when you click on an element, a fragment opens where the text from this element will be displayed. I've read similar threads but I don't understand. We need an interface in the adapter, the method of which we override in the holder ... but how to pass data to the activity so that it forwards them to the next fragment?
class NotesFragment : Fragment() {
private var binding: FragmentNotesBinding? = null
private val adapter = NoteAdapter()
interface OpenFragment {
fun addEditFragment() {
}
fun addDetailFragment(detail: String) {}
}
companion object {
fun newInstance() = NotesFragment()
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentNotesBinding.inflate(layoutInflater)
return binding?.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding!!.rcList.layoutManager = GridLayoutManager(context, 2)
binding!!.rcList.adapter = adapter
binding!!.bAdd.setOnClickListener() {
(activity as OpenFragment).addEditFragment()
}
}
override fun onResume() {
super.onResume()
setFragmentResultListener("key") { key, bundle ->
val result = bundle.getString("bundleKey")
if (result != null) {
adapter.addNote(result)
}
}
}
override fun onDestroy() {
super.onDestroy()
binding = null
}
}
Answer the question
In order to leave comments, you need to log in
Need interface in adapter
, whose method we override in the holder
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question