0
0
007hrist2020-11-13 04:34:12
Java
007hrist, 2020-11-13 04:34:12

How to call a method from MainActivity when clicking on ReryclerView element?

I did it like this, but it doesn’t work, I don’t understand how to use point 4, what needs to be passed to the constructor from the adapter so that it doesn’t equal null?

public interface OnRecyclerViewItemClickListener {
    void onClick();
}

public class MainActivity extends AppCompatActivity implements OnRecyclerViewItemClickListener {

    ...

    @Override
    public void onClick() {

    }
}

In adapter:

private OnRecyclerViewItemClickListener mClickListener;


public MyAwesomeAdapter(OnRecyclerViewItemClickListener clickListener) {
    mClickListener = clickListener;
}


if (mClickListener != null) {
    mClickListener.onClick();
}


How to set a click event handler?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Zakharov, 2020-11-13
@007hrist

In this option, when creating an adapter in an activity, you need to pass this to the constructor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question