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?