Skip to content

Commit

Permalink
Merge pull request #9 from MaximeJallu/feat_add_itemclicklistener
Browse files Browse the repository at this point in the history
feat(adapter): add kotlin adapter
  • Loading branch information
MaximeJallu authored Sep 29, 2017
2 parents 9439131 + 9016296 commit 507629a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions adapter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ android {
// applicationId "com.android.jmaxime.com.android.jmaxime.adapter"
minSdkVersion 23
targetSdkVersion 26
versionCode 1710291743 /*YYMMDDHHMM*/
versionName "1.2"
versionCode 1710291830 /*YYMMDDHHMM*/
versionName "1.3"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
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);
}
}

0 comments on commit 507629a

Please sign in to comment.