-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from MaximeJallu/feat_add_itemclicklistener
feat(adapter): add kotlin adapter
- Loading branch information
Showing
2 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
adapter/src/main/java/com/android/jmaxime/utils/RecyclerItemClickListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.android.jmaxime.utils; | ||
|
||
|
||
import android.support.v7.widget.RecyclerView; | ||
import android.support.v7.widget.RecyclerView.OnChildAttachStateChangeListener; | ||
import android.view.View; | ||
import android.view.View.OnClickListener; | ||
|
||
public class RecyclerItemClickListener implements OnChildAttachStateChangeListener { | ||
private final RecyclerView mRecycler; | ||
private final RecyclerItemClickListener.OnRecyclerOnItemClickListener mListener; | ||
|
||
public RecyclerItemClickListener(RecyclerView recyclerView, RecyclerItemClickListener.OnRecyclerOnItemClickListener listener) { | ||
this.mRecycler = recyclerView; | ||
this.mListener = listener; | ||
} | ||
|
||
public static void affectOnItemClick(RecyclerView recyclerView, RecyclerItemClickListener.OnRecyclerOnItemClickListener listener) { | ||
recyclerView.addOnChildAttachStateChangeListener(new RecyclerItemClickListener(recyclerView, listener)); | ||
} | ||
|
||
public void onChildViewAttachedToWindow(View view) { | ||
view.setOnClickListener(new OnClickListener() { | ||
public void onClick(View v) { | ||
RecyclerItemClickListener.this.setOnChildAttachedToWindow(v); | ||
} | ||
}); | ||
} | ||
|
||
private void setOnChildAttachedToWindow(View v) { | ||
if(v != null && this.mListener != null) { | ||
int position = this.mRecycler.getChildLayoutPosition(v); | ||
if(position >= 0) { | ||
this.mListener.onItemClick(position, v); | ||
} | ||
} | ||
|
||
} | ||
|
||
public void onChildViewDetachedFromWindow(View view) { | ||
if(view != null) { | ||
view.setOnClickListener((OnClickListener)null); | ||
} | ||
} | ||
|
||
public interface OnRecyclerOnItemClickListener { | ||
void onItemClick(int var1, View var2); | ||
} | ||
} |