L
L
LilSem2017-05-18 00:19:31
Java
LilSem, 2017-05-18 00:19:31

How to open fragment from RecyclerView adapter?

I have an adapter that populates a RecyclerView. After reading the forums, I realized that the click handler must also be done in the adapter. How to open fragment on click on RecyclerView item?

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

Is this not working? writes cannot resolve method getFragmentManager()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-05-18
@zagayevskiy

The adapter is responsible for populating the recyclerview. Need to create an interface

interface ItemClickListener{
    void onItemClick(ItemType item);
}

ItemType - element type, or, for example, int - position number.
Create a setotemclicklistener(ItemClickListener listener) adapter method where to store the listener.
When clicking on items, call the onItemClick method with the appropriate parameter.
In the place where the adapter is created (fragment, activity, etc) - create a listener and pass it to the adapter. Open the desired fragment in the callback.
>> writes cannot resolve method
getFragmentManager()
This is because the adapter does not have this method, the activity has it, for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question