This is not an answer to your request, but the best way to handle this scenario.
Use callback methods.
In your activity:
This will implement the interface that we have in our Adapter . In this example, it will be called when the user clicks on an item in the RecyclerView .
public class MyActivity extends Activity implements AdapterCallback { private MyAdapter mMyAdapter; @Override public void onMethodCallback() {
In your adapter:
In Activity, we initiated our Adapter and passed this as an argument to the constructor. This will launch our interface for our callback method. You can see that we use our callback method for custom clicks.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private AdapterCallback mAdapterCallback; public MyAdapter(Context context) { try { this.mAdapterCallback = ((AdapterCallback) context); } catch (ClassCastException e) { throw new ClassCastException("Activity must implement AdapterCallback."); } } @Override public void onBindViewHolder(final MyAdapter.ViewHolder viewHolder, final int i) {
Provided: Invoking an activity method from the adapter
Anoop m
source share