K
K
Koshkasobaka2021-06-17 15:02:01
Android
Koshkasobaka, 2021-06-17 15:02:01

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
    }
}


Adapter:


class NoteAdapter : RecyclerView.Adapter() {
private val noteList = ArrayList()

class NoteHolder(item: View) : RecyclerView.ViewHolder(item) {
private val binding = MyNoteItemBinding.bind(item)
fun bind(note: String) = with(binding) {
tvMessage.text = note
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.my_note_item, parent, false)
return NoteHolder(view)
}

override fun onBindViewHolder(holder: NoteHolder, position: Int) {
holder.bind(noteList[position])
}

override fun getItemCount(): Int {
return noteList.size
}

fun addNote(note: String) {
noteList.add(note)
notifyDataSetChanged()
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-06-17
@Koshkasobaka

Need interface in adapter

Yes.
, whose method we override in the holder

No.
The instance of this interface must come (to the constructor or setter) from outside, and in the holder you need a ClickListener, in which the method of this interface of yours will be called. You can pass arbitrary data to the method, which is in the holder (you can remember the last element attached to this holder, for example).
This interface is implemented by the person who owns the adapter, such as an activity.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question