From 9016296b7aed963d5c2907074817bb564d89ce30 Mon Sep 17 00:00:00 2001 From: Maxime Jallu Date: Fri, 29 Sep 2017 18:36:21 +0200 Subject: [PATCH] feat(adapter): add kotlin adapter --- adapter/build.gradle | 4 +- .../utils/RecyclerItemClickListener.java | 49 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 adapter/src/main/java/com/android/jmaxime/utils/RecyclerItemClickListener.java diff --git a/adapter/build.gradle b/adapter/build.gradle index e6833cf..0d03594 100644 --- a/adapter/build.gradle +++ b/adapter/build.gradle @@ -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" diff --git a/adapter/src/main/java/com/android/jmaxime/utils/RecyclerItemClickListener.java b/adapter/src/main/java/com/android/jmaxime/utils/RecyclerItemClickListener.java new file mode 100644 index 0000000..30baad4 --- /dev/null +++ b/adapter/src/main/java/com/android/jmaxime/utils/RecyclerItemClickListener.java @@ -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); + } +}