2

i'm working my way through an android course online and i have some questions regarding the code, specifically about interfaces.

The Code:

package com.test.personalnotes;

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

/**
 * Created by Admin on 24/12/15.
 */
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {

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

        public void onItemLongClick(View view, int position);
    }

    private OnItemClickListener mListener; <-- An interface variable..?
    private GestureDetector mGestureDetector;

    public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            public void onLongPress(MotionEvent motionEvent) {
                //Gets the childview of the recyclerview (I.e What is beneath the area which was pressed)
                View childView = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
                if (childView != null && mListener != null) {
                    mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView)); <-- Code i'm unsure of No.1
                }
            }

        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
        View childview = recyclerView.findChildViewUnder(motionEvent.getX(),motionEvent.getY());
        //If there is something under the area tapped,mListener is not ull and a touch event was registered
        if(childview != null && mListener != null && mGestureDetector.onTouchEvent(motionEvent)){
            mListener.onItemClick(childview,recyclerView.getChildAdapterPosition(childview)); <-- Code i'm unsure of No.2
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}

From the code snipper above, where we are implementing a listener for a recyclerview, we create an interface OnItemClickListener with 2 methods and a variable mListener of type OnItemClickListener(The interface).

From what i understand of interfaces, from http://www.dummies.com/how-to/content/what-is-an-interface-in-java.html & Is there more to an interface than having the correct methods , i know that the methods defined in an interface have to be implemented when the interface is implemented.

However, in the code snippet, we are actually able to create a variable of type OnItemClickListener(The interface)?

Qn1.

Do i assume the by creating an interface variable i am actually implementing it?

Qn2.

I do not actually have to implement the methods onItemClick & onItemLongClick in the interface? I say this because when i comment out one of the lines of code which i'm unsure of, i do not get any errors from android studio

Qn 3.

With the following line of code,

mListener.onItemClick(childview,recyclerView.getChildAdapterPosition(childview));

where we are instantiating the method, why are we able to do so without actually writing the code body for the method?

E.g

mListener.onItemClick(childview,recyclerView.getChildAdapterPosition(childview)){
    //Enter code here for handling the actual item click
    //For example, textView.setText(text);
};

Am i severely misunderstanding something about interfaces?

Community
  • 1
  • 1
Kenneth .J
  • 1,433
  • 8
  • 27
  • 49

1 Answers1

3

Do i assume the by creating an interface variable i am actually implementing it?

No, the variable contains null until you assign to it an instance of a class that implements that interface, which you do in the line - mListener = listener;.

I do not actually have to implement the methods onItemClick & onItemLongClick in the interface?

Nowhere in the code you posted are you implementing the OnItemClickListener interface. When you create a class that implements that interface, you'll have to implement all of its methods (unless that class would be abstract).

As for the last question :

public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
    mListener = listener;
    ...

Your RecyclerItemClickListener constructor accepts the listener argument, which is a reference to an instance of a class that implements OnItemClickListener, and therefore implements all the methods of that interface. It then assigns it to mListener, so mListener can be used to execute any method of that interface.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Thanks for taking the time to answer, just to clarify, am i correct in saying that because the mListener variable is assigned the value of listener, which is an instance of a class(A) that implements OnItemClickListener, when i call the line mListener.onItemLongClick, i am actually calling the onItemLongClick method as defined in class A, with all it's associated code.(E.g for handling the itemlongclick?) – Kenneth .J Dec 24 '15 at 13:59