A somewhat unusual implementation is proposed in another answer .. I will write a more classic solution.
For a start, whatβs wrong with calling the activation methods from the adapter and why itβs bad. The fact is that the activator is a kontroller of what is displayed on the screen and it should act as the initiator of the actions of the widgets located on it (the adapter is part of the widget for displaying the list). If you turn to an abstract example, then when you call the activation methods from the adapter, you get something like the fact that the mailbox "sucks" the apartment with you, so that you can check your mail, instead of you going there to look. This solution does not look very much.
The general sense is that the control should initiate actions, and not events affect the control (the remote control programs in the TV, not the program to switch channels)
The methods of interaction with the adapter depend on the type of interaction - whether an event occurs in the widget (for example, click on an element or in an abstract example - whether the mail arrived in the mailbox), which needs to be activated or activated, some information / action is required from the widget, for example, the data displayed in a widget or an update in the list.
In the first case, a callback mechanism is used (callback interface). In the abstract example, we will install a sensor in the box that coordinates the presence of mail.
This mechanism is designed so that by calling the interface method in one place of the code, it is automatically called in another place that implements this callback interface. In this case, the arguments received in the method of communication initiator are transferred to the receiver method and you can transfer data associated with the event (for example, record ID).
Typical callback.
In the adapter:
class SomeAdapter extends RecyclerView.Adapter <Data, SomeAdapter.ItemHolder> { // ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΡΠ°ΠΌ ΠΈΠ½ΡΠ΅ΡΡΠ΅ΠΉΡ ΠΈ ΡΠΊΠ°Π·ΡΠ²Π°Π΅ΠΌ ΠΌΠ΅ΡΠΎΠ΄ ΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°Π²Π°Π΅ΠΌΡΠ΅ ΠΈΠΌ Π°ΡΠ³ΡΠΌΠ΅Π½ΡΡ // View Π½Π° ΠΊΠΎΡΠΎΡΠΎΠΌ ΠΏΡΠΎΠΈΠ·ΠΎΡΠ»ΠΎ ΡΠΎΠ±ΡΡΠΈΠ΅ ΠΈ ΠΏΠΎΠ·ΠΈΡΠΈΡ ΡΡΠΎΠ³ΠΎ View interface OnCardClickListener { void onCardClick(View view, int position); } // ΡΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΠΎΠ»Π΅ ΠΎΠ±ΡΠ΅ΠΊΡΠ°-ΠΊΠΎΠ»Π±ΡΠΊΠ° private static OnCardClickListener mListener; public SomeAdapter (Data data) { // ΠΊΠΎΠ½ΡΡΡΡΠΊΡΠΎΡ Π°Π΄Π°ΠΏΡΠ΅ΡΠ° } @Override public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) { .... return new ItemHolder(v); } @Override public void onBindViewHolder( ItemHolder holder, int position) { ..... } // ΠΌΠ΅ΡΠΎΠ΄-ΡΠ΅ΡΡΠ΅Ρ Π΄Π»Ρ ΠΏΡΠΈΠ²ΡΠ·ΠΊΠΈ ΠΊΠΎΠ»Π±ΡΠΊΠ° ΠΊ ΠΏΠΎΠ»ΡΡΠ°ΡΠ΅Π»Ρ ΡΠΎΠ±ΡΡΠΈΠΉ public void setOnCardClickListener(OnCardClickListener listener) { mListener = listener; } public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ CardView mCard; public ItemHolder(View v) { super(v); mCard = (CardView) v.findViewById(R.id.bell_card); // ΡΠ°ΠΌΠΎ ΡΠΎΠ±ΡΡΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡ (ΠΊΠ»ΠΈΠΊ Π½Π° ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ΅) mCard.setOnClickListener(this); } // ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΡΠΎΠ±ΡΡΠΈΡ @Override public void onClick(View v) { // Π²ΡΠ·ΠΎΠ² ΠΌΠ΅ΡΠΎΠ΄Π°-ΠΊΠΎΠ»Π±ΡΠΊΠ° ΠΈ Π½Π°Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΠΏΠ΅ΡΠ΅Π΄Π°Π²Π°Π΅ΠΌΡΡ
Π·Π½Π°ΡΠ΅Π½ΠΈΠΉ Π°ΡΠ³ΡΠΌΠ΅Π½ΡΠ°ΠΌ int position = getAdapterPosition(); mListener.onCardClick(v, position); } } }
In activit:
// ΠΈΠΌΠΏΠ»Π΅ΠΌΠ΅Π½ΡΠΈΡΡΠ΅ΠΌ Π½Π°Ρ ΠΈΠ½ΡΠ΅ΡΡΠ΅ΠΉΡ ΠΎΠ±ΡΠ°ΡΠ½ΠΎΠ³ΠΎ Π²ΡΠ·ΠΎΠ²Π° Π² Π°ΠΊΡΠΈΠ²ΠΈΡΠΈ public class SomeActiviy extends Activity implements SomeAdapter.OnCardClickListener { Data data; ... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mAdapter = new SomeAdapter(data); // ΠΏΠΎΠ΄ΠΏΠΈΡΡΠ²Π°Π΅ΠΌ Π½Π°ΡΡ Π°ΠΊΡΠΈΠ²ΠΈΡΠΈ Π½Π° ΡΠΎΠ±ΡΡΠΈΡ ΠΊΠΎΠ»Π±ΡΠΊΠ° mAdapter.setOnCardClickListener(this); mList.setAdapter(mAdapter); } // ΠΌΠ΅ΡΠΎΠ΄, ΠΊΠΎΡΠΎΡΡΠΉ ΠΏΠΎΠ»ΡΡΠΈΡ ΡΠΎΠ±ΡΡΠΈΡ ΠΈΠ· Π½Π°ΡΠ΅Π³ΠΎ ΠΊΠΎΠ»Π±ΡΠΊΠ° @Override public void onCardClick(View view,final int pos) { // Π·Π΄Π΅ΡΡ ΠΎΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Π΅ΠΌ ΡΠΎΠ±ΡΡΠΈΠ΅, Π·Π½Π°ΡΠ΅Π½ΠΈΡ View ΠΈ pos // ΡΠ°Π²Π½Ρ Π·Π½Π°ΡΠ΅Π½ΠΈΡΠΌ, ΠΏΡΠΈΡΠ²ΠΎΠ΅Π½Π½ΡΠΌ Π² Π°Π΄Π°ΠΏΡΠ΅ΡΠ΅ } }
The second option is much simpler, we just create methods in the adapter that will return the data we need or will cause certain actions in the adapter when they are required to be activated. That is, itβs not the adapter that calls activation methods, but it activates the adapter methods when it needs information / actions from it:
in adapter:
class SomeAdapter extends RecyclerView.Adapter <Data, SomeAdapter.ItemHolder> { // ΠΌΠ΅ΡΠΎΠ΄ ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΡ ΡΠΏΠΈΡΠΊΠ° ΠΏΠΎ Π½ΠΎΠ²ΠΎΠΌΡ Π½Π°Π±ΠΎΡΡ Π΄Π°Π½Π½ΡΡ
public void notifyData(Data result) { mData = result; notifyDataSetChanged(); } // ΠΌΠ΅ΡΠΎΠ΄ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΡΠ°Π·ΠΌΠ΅ΡΠ° ΡΠΏΠΈΡΠΊΠ° public int getSize() { return mData.size(); } }
In activit:
public class SomeActiviy extends Activity { Data data; ... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mAdapter = new SomeAdapter(data); mList.setAdapter(mAdapter); // Π°ΠΊΡΠΈΠ²ΠΈΡΠΈ ΡΡΠ΅Π±ΡΠ΅ΡΡΡ ΡΠ°Π·ΠΌΠ΅Ρ ΡΠΏΠΈΡΠΊΠ° int size = mAdapter.getSize(); // Π°ΠΊΡΠΈΠ²ΠΈΡΠΈ ΡΡΠ΅Π±ΡΠ΅ΡΡΡ ΠΎΠ±Π½ΠΎΠ²ΠΈΡΡ ΡΠΏΠΈΡΠΎΠΊ Ρ Π²Π½ΠΎΠ²Ρ ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΡΠΌΠΈ Π΄Π°Π½Π½ΡΠΌΠΈ Data data = getData(); mAdapter.notifyData(data); } }