1

I was following this article to set a click listener on recyclerview, but I'm confused on what does the following interface does inside the RecyclerItemClickListenerclass:

  public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }

Here is the complete class:

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {

    private OnItemClickListener clickListener;

    public interface OnItemClickListener {
        public void onItemClick(View view, int position);
    }

    GestureDetector gestureDetector;

    public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
        clickListener = listener;
        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

        View childView = rv.findChildViewUnder(e.getX(), e.getY());

        if(childView != null && clickListener != null && gestureDetector.onTouchEvent(e)){
            clickListener.onItemClick(childView,rv.getChildAdapterPosition(childView));
        }

        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    }
}

This is how it's used on the Activity:

recyclerView.addOnItemTouchListener(
        new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(HomeActivity.this, " " + position, Toast.LENGTH_SHORT).show();
            }
        })
);

It's working fine but I really want to know what is the responsibility of the interface or what it's doing. Thanks in advance!

Jeka
  • 1,600
  • 3
  • 22
  • 36

3 Answers3

1

An inner interface and inner classes are used when they make sense for example in here only class RecyclerItemClickListener uses interface OnItemClickListenerso it makes sense it is inside class RecyclerItemClickListener.

So when you want to emphasizes that something makes no sense outside something you nest it or put it inside the class.

Notice how in your activity you call: new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() this part new RecyclerItemClickListener.OnItemClickListener()emphasized that it is an interface inside class RecyclerItemClickListener, seems this interface doesn't make sense to be outside since it's not use by anything else so it is inside RecyclerItemClickListener class.

Look this posts for more info: here, here and here

Community
  • 1
  • 1
CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186
1
Hey if you see the constructor of the RecyclerItemClickListener class . the OnItemClickListener is initialized inside the constructor .

and  when we are calling the RecyclerItemClickListener. we are providing second argument for the initialization of the listener.


and inside the onInterceptTouchEvent method we are calling the onItemClick(View view,int position ) method of the listener by filling appropriate data .

you are sending reference when you are calling the RecyclerItemClickListener.

Vivek Pratap Singh
  • 1,564
  • 16
  • 26
1

If you know how interface works you would understand it easily. Since the RecyclerView doesn't have a click listener, this guy made his own class that is implementing RecyclerView.OnItemTouchListener. So now when user touches the RecyclerView item, he catches the touch in code in onInterceptTouchEvent.

So now he comes to know that user clicked in item, so he makes an interface that is OnItemClickListener in the same class, and you are implementing that interface in your Activity class and you have to write the code that what happens after the click.

But how is it fired when user clicks?

You see the line

clickListener.onItemClick(childView,rv.getChildAdapterPosition(childView));

This line in turn calls your activity's onItemClickListener, so it call backs to your activity so you know that user has touched the item.

Hope you understood :)

rusted brain
  • 1,052
  • 10
  • 23