From 2e2fef62a2fa6bc8dcd20b4df48ae2f7bf16400d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hor=C3=A1cio=20Com=C3=A9?= Date: Fri, 14 Dec 2018 16:54:30 +0200 Subject: [PATCH] migrating to kotling --- ...\303\241vio_Com\303\251_J\303\272nior.xml" | 6 ++ .idea/copyright/profiles_settings.xml | 7 ++ .idea/markdown-navigator.xml | 83 +++++++++++++++++++ .../markdown-navigator/profiles_settings.xml | 3 + README.md | 81 ++++++++---------- app/.gitignore | 1 - app/build.gradle | 23 ----- app/proguard-rules.pro | 21 ----- .../ExampleInstrumentedTest.java | 27 ------ app/src/main/AndroidManifest.xml | 7 -- .../ExampleUnitTest.java | 17 ---- build.gradle | 5 +- simplerecyclerviewtouchlistener/build.gradle | 11 ++- .../MyGestureListener.java | 55 ------------ ...SimpleRecyclerViewOnItemTouchListener.java | 81 ------------------ .../MyGestureListener.kt | 50 +++++++++++ .../RecyclerViewUtils.kt | 23 +++++ .../SimpleRecyclerViewOnItemTouchListener.kt | 55 ++++++++++++ 18 files changed, 271 insertions(+), 285 deletions(-) create mode 100644 ".idea/copyright/Hor\303\241cio_Fl\303\241vio_Com\303\251_J\303\272nior.xml" create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/markdown-navigator.xml create mode 100644 .idea/markdown-navigator/profiles_settings.xml delete mode 100644 app/.gitignore delete mode 100644 app/build.gradle delete mode 100644 app/proguard-rules.pro delete mode 100644 app/src/androidTest/java/com/wordpress/beendora/recyclerviewtouchlistener/ExampleInstrumentedTest.java delete mode 100644 app/src/main/AndroidManifest.xml delete mode 100644 app/src/test/java/com/wordpress/beendora/recyclerviewtouchlistener/ExampleUnitTest.java delete mode 100644 simplerecyclerviewtouchlistener/src/main/java/com/wordpress/beendora/simplerecyclerviewtouchlistener/MyGestureListener.java delete mode 100644 simplerecyclerviewtouchlistener/src/main/java/com/wordpress/beendora/simplerecyclerviewtouchlistener/SimpleRecyclerViewOnItemTouchListener.java create mode 100644 simplerecyclerviewtouchlistener/src/main/java/io/github/horaciocome1/simplerecyclerviewtouchlistener/MyGestureListener.kt create mode 100644 simplerecyclerviewtouchlistener/src/main/java/io/github/horaciocome1/simplerecyclerviewtouchlistener/RecyclerViewUtils.kt create mode 100644 simplerecyclerviewtouchlistener/src/main/java/io/github/horaciocome1/simplerecyclerviewtouchlistener/SimpleRecyclerViewOnItemTouchListener.kt diff --git "a/.idea/copyright/Hor\303\241cio_Fl\303\241vio_Com\303\251_J\303\272nior.xml" "b/.idea/copyright/Hor\303\241cio_Fl\303\241vio_Com\303\251_J\303\272nior.xml" new file mode 100644 index 0000000..778f95d --- /dev/null +++ "b/.idea/copyright/Hor\303\241cio_Fl\303\241vio_Com\303\251_J\303\272nior.xml" @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..daeb00b --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/markdown-navigator.xml b/.idea/markdown-navigator.xml new file mode 100644 index 0000000..8d6f051 --- /dev/null +++ b/.idea/markdown-navigator.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/markdown-navigator/profiles_settings.xml b/.idea/markdown-navigator/profiles_settings.xml new file mode 100644 index 0000000..57927c5 --- /dev/null +++ b/.idea/markdown-navigator/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/README.md b/README.md index 18805f9..06dfe35 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Simple RecyclerView Touch Listener -[![](https://jitpack.io/v/horaciocome1/simple-recyclerview-touch-listener.svg)](https://jitpack.io/#horaciocome1/simple-recyclerview-touch-listener)[![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0) [![API](https://img.shields.io/badge/API-7%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=7) +[![](https://jitpack.io/v/horaciocome1/simple-recyclerview-touch-listener.svg)](https://jitpack.io/#horaciocome1/simple-recyclerview-touch-listener)[![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0) [![API](https://img.shields.io/badge/API-14%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=14) ## Getting Started -This is a library that abstracts, and completely hide, the GestureDetector part of an recyclerview click, and press, events implementation. Leaving to the developer only the task of implementing what happens when such events occurs. +Android library that abstracts, and completely hide, the GestureDetector part of an recyclerview click, and press, events implementation. Leaving to the developer only the task of implementing what happens when such events occurs. Compatible with androidx. ## Pre-requesites @@ -22,7 +22,7 @@ The next task is to add the dependecy to your _app_ `build.gradle` file. ```gradle dependencies { ... - implementation 'com.github.horaciocome1:simple-recyclerview-touch-listener:0.1.4' + implementation 'com.github.horaciocome1:simple-recyclerview-touch-listener:0.1.5' } ``` Now you ready to go. Except that you should _**sync your project**_ first. @@ -39,58 +39,43 @@ Dont panic, you can use version 0.1.0 with the old support libraries. But dont e Do not try to implement as follow. The object `SimpleRecyclerViewOnItemTouchListener` is still the same. Just instanciate the class passing necessary arguments. Makeuseof code completion to help you. I am wondering if it is necessary to do a separate lib for suppor appcompat. Email me if you thinking on that. ## How to use -Intanciate a class `SimpleRecyclerViewOnItemTouchListener` by using its own builder. -```java -SimpleRecyclerViewOnItemTouchListener onItemTouchListener = new SimpleRecyclerViewOnItemTouchListener.Builder() - .setOnItemClickListener(new SimpleRecyclerViewOnItemTouchListener.OnItemClickListener() { - @Override - public void onClick(View view, int position) { - // handle click event - } - }) - .setOnItemDoubleClickListener(new SimpleRecyclerViewOnItemTouchListener.OnItemDoubleClickListener() { - @Override - public void onDoubleClick(View view, int position) { - // handle double click events - } - }) - .setOnItemLongPressListener(new SimpleOnItemTouch.OnItemLongPressListener() { - @Override - public void onLongPress(View view, int position) { - // handle long press event - } - }) - .build(this, mRecyclerView); - - // adding the listener to its corresponding recyclerview - mRecyclerView.addOnItemTouchListener(onItemTouchListener); +I suggest calling custom **addSimpleTouchListener()** recyclerview extension, because its more easy to get the _recyclerview_. +```kotlin +recyclerView.addSimpleTouchListener( SimpleRecyclerViewOnItemTouchListener().apply { + setOnItemClickListener { view, i -> /* handle clicks */ } + setOnItemDoubleClickListener { view, i -> /* handle double clicks */ } + setOnItemLongPressListener { view, i -> /* handle long presses */ } +}) +``` + +If you want to call default **addOnItemTouchListener()**, you need to specify the recyclerview. +```kotlin +recyclerview.addSimpleTouchListener( SimpleRecyclerViewOnItemTouchListener().apply { + setOnItemClickListener { view, i -> /* handle clicks */ } + setOnItemDoubleClickListener { view, i -> /* handle double clicks */ } + setOnItemLongPressListener { view, i -> /* handle long presses */ } + this.recyclerview = recyclerview +}) ``` + Its not mandatory to implement all at once. Pheraphs you only need to handle long presses. In that case you should only implement `setOnItemLongPressListener( ... )`. -```java -SimpleRecyclerViewOnItemTouchListener onItemTouchListener = new SimpleRecyclerViewOnItemTouchListener.Builder() - .setOnItemLongPressListener(new SimpleOnItemTouch.OnItemLongPressListener() { - @Override - public void onLongPress(View view, int position) { - // handle long press event - } - }) - .build(this, mRecyclerView); +```kotlin +this.addSimpleTouchListener( SimpleRecyclerViewOnItemTouchListener().apply { + setOnItemClickListener { view, i -> /* handle clicks */ } +}) ``` -### Kotlin +### Java +I'll submit soon. ```kotlin -recyclerView.addOnItemTouchListener(SimpleRecyclerViewOnItemTouchListener.Builder() - .setOnItemClickListener { view, position -> // handle clicks } - .build(view.context, recyclerView) -) + ``` ### Troubleshooting -Notice that it is crucial to call `build( ... )` so that the listener it self could be initialized. -All listeners not specified or implemented will not be called. In above exemple, click and double click events are totally not handled at all. And that's why it makes no sense only calling `build( ... )`. -```java -SimpleRecyclerViewOnItemTouchListener onItemTouchListener = new SimpleRecyclerViewOnItemTouchListener.Builder() - .build(this, mRecyclerView); +Naturally, you need to implement those callback or nothing is done. +```kotlin +// this is what you need to a void +recyclerView.addSimpleTouchListener(SimpleRecyclerViewOnItemTouchListener()) ``` ### "Build or sinchronization failed!" @@ -115,6 +100,6 @@ Please reference to the part on the start where i talked about support libraries I am open to suggestions of any kind. Please be expressive, so others so we'all will be able to understand each other! -# Simple RecyclerView Utils +## Simple RecyclerView Utils This is part of a serie of libraries that pretend to make recyclerview usage more easy. For a adapter please see [Simple RecyclerView Adapter](https://github.com/horaciocome1/simple-recyclerview-adapter) \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore deleted file mode 100644 index 796b96d..0000000 --- a/app/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/build diff --git a/app/build.gradle b/app/build.gradle deleted file mode 100644 index 1b5e3e6..0000000 --- a/app/build.gradle +++ /dev/null @@ -1,23 +0,0 @@ -apply plugin: 'com.android.application' - -android { - compileSdkVersion 28 - defaultConfig { - applicationId "com.wordpress.beendora.recyclerviewtouchlistener" - minSdkVersion 14 - targetSdkVersion 28 - versionCode 1 - versionName "0.1.1" - } - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } - } -} - -dependencies { - implementation fileTree(dir: 'libs', include: ['*.jar']) - testImplementation 'junit:junit:4.12' -} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro deleted file mode 100644 index f1b4245..0000000 --- a/app/proguard-rules.pro +++ /dev/null @@ -1,21 +0,0 @@ -# Add project specific ProGuard rules here. -# You can control the set of applied configuration files using the -# proguardFiles setting in build.gradle. -# -# For more details, see -# http://developer.android.com/guide/developing/tools/proguard.html - -# If your project uses WebView with JS, uncomment the following -# and specify the fully qualified class name to the JavaScript interface -# class: -#-keepclassmembers class fqcn.of.javascript.interface.for.webview { -# public *; -#} - -# Uncomment this to preserve the line number information for -# debugging stack traces. -#-keepattributes SourceFile,LineNumberTable - -# If you keep the line number information, uncomment this to -# hide the original source file name. -#-renamesourcefileattribute SourceFile diff --git a/app/src/androidTest/java/com/wordpress/beendora/recyclerviewtouchlistener/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/wordpress/beendora/recyclerviewtouchlistener/ExampleInstrumentedTest.java deleted file mode 100644 index 16ca8dd..0000000 --- a/app/src/androidTest/java/com/wordpress/beendora/recyclerviewtouchlistener/ExampleInstrumentedTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.wordpress.beendora.recyclerviewtouchlistener; - -import android.content.Context; -import android.support.test.InstrumentationRegistry; -import android.support.test.runner.AndroidJUnit4; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import static org.junit.Assert.*; - -/** - * Instrumented test, which will execute on an Android device. - * - * @see Testing documentation - */ -@RunWith(AndroidJUnit4.class) -public class ExampleInstrumentedTest { - @Test - public void useAppContext() { - // Context of the app under test. - Context appContext = InstrumentationRegistry.getTargetContext(); - - assertEquals("com.wordpress.beendora.recyclerviewtouchlistener", - appContext.getPackageName()); - } -} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml deleted file mode 100644 index 94c12ab..0000000 --- a/app/src/main/AndroidManifest.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - diff --git a/app/src/test/java/com/wordpress/beendora/recyclerviewtouchlistener/ExampleUnitTest.java b/app/src/test/java/com/wordpress/beendora/recyclerviewtouchlistener/ExampleUnitTest.java deleted file mode 100644 index fe81fd2..0000000 --- a/app/src/test/java/com/wordpress/beendora/recyclerviewtouchlistener/ExampleUnitTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.wordpress.beendora.recyclerviewtouchlistener; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Example local unit test, which will execute on the development machine (host). - * - * @see Testing documentation - */ -public class ExampleUnitTest { - @Test - public void addition_isCorrect() { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 8d3ef8e..4189a0c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,15 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - + ext.kotlin_version = '1.3.11' + repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.2.1' - + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/simplerecyclerviewtouchlistener/build.gradle b/simplerecyclerviewtouchlistener/build.gradle index 6c9c298..f1e8347 100644 --- a/simplerecyclerviewtouchlistener/build.gradle +++ b/simplerecyclerviewtouchlistener/build.gradle @@ -1,12 +1,13 @@ apply plugin: 'com.android.library' +apply plugin: 'kotlin-android' android { compileSdkVersion 28 defaultConfig { - minSdkVersion 7 + minSdkVersion 14 targetSdkVersion 28 - versionCode 5 - versionName '0.1.4' + versionCode 6 + versionName '0.1.5' testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -28,4 +29,8 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.0.2' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +} +repositories { + mavenCentral() } diff --git a/simplerecyclerviewtouchlistener/src/main/java/com/wordpress/beendora/simplerecyclerviewtouchlistener/MyGestureListener.java b/simplerecyclerviewtouchlistener/src/main/java/com/wordpress/beendora/simplerecyclerviewtouchlistener/MyGestureListener.java deleted file mode 100644 index a67c6c2..0000000 --- a/simplerecyclerviewtouchlistener/src/main/java/com/wordpress/beendora/simplerecyclerviewtouchlistener/MyGestureListener.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.wordpress.beendora.simplerecyclerviewtouchlistener; - -import android.view.GestureDetector; -import android.view.MotionEvent; -import android.view.View; - -import androidx.recyclerview.widget.RecyclerView; - -class MyGestureListener extends GestureDetector.SimpleOnGestureListener { - - private RecyclerView mRecyclerView; - private SimpleRecyclerViewOnItemTouchListener.OnItemClickListener mOnItemClickListener; - private SimpleRecyclerViewOnItemTouchListener.OnItemDoubleClickListener mOnItemDoubleClickListener; - private SimpleRecyclerViewOnItemTouchListener.OnItemLongPressListener mOnItemLongPressListener; - - public MyGestureListener(RecyclerView mRecyclerView, - SimpleRecyclerViewOnItemTouchListener.OnItemClickListener mOnItemClickListener, - SimpleRecyclerViewOnItemTouchListener.OnItemDoubleClickListener mOnItemDoubleClickListener, - SimpleRecyclerViewOnItemTouchListener.OnItemLongPressListener mOnItemLongPressListener) { - this.mRecyclerView = mRecyclerView; - this.mOnItemClickListener = mOnItemClickListener; - this.mOnItemDoubleClickListener = mOnItemDoubleClickListener; - this.mOnItemLongPressListener = mOnItemLongPressListener; - } - - @Override - public boolean onSingleTapUp(MotionEvent e) { - if (!(mRecyclerView == null)) { - View view = mRecyclerView.findChildViewUnder(e.getX(), e.getY()); - if (!(view == null) && !(mOnItemClickListener == null)) - mOnItemClickListener.onClick(view, mRecyclerView.getChildAdapterPosition(view)); - } - return true; - } - - @Override - public void onLongPress(MotionEvent e) { - if (!(mRecyclerView == null)) { - View view = mRecyclerView.findChildViewUnder(e.getX(), e.getY()); - if (!(view == null) && !(mOnItemLongPressListener == null)) - mOnItemLongPressListener.onLongPress(view, mRecyclerView.getChildAdapterPosition(view)); - } - } - - @Override - public boolean onDoubleTap(MotionEvent e) { - if (!(mRecyclerView == null)) { - View view = mRecyclerView.findChildViewUnder(e.getX(), e.getY()); - if (!(view == null) && !(mOnItemDoubleClickListener == null)) - mOnItemDoubleClickListener.onDoubleClick(view, mRecyclerView.getChildAdapterPosition(view)); - } - return true; - } - -} diff --git a/simplerecyclerviewtouchlistener/src/main/java/com/wordpress/beendora/simplerecyclerviewtouchlistener/SimpleRecyclerViewOnItemTouchListener.java b/simplerecyclerviewtouchlistener/src/main/java/com/wordpress/beendora/simplerecyclerviewtouchlistener/SimpleRecyclerViewOnItemTouchListener.java deleted file mode 100644 index 5b14187..0000000 --- a/simplerecyclerviewtouchlistener/src/main/java/com/wordpress/beendora/simplerecyclerviewtouchlistener/SimpleRecyclerViewOnItemTouchListener.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.wordpress.beendora.simplerecyclerviewtouchlistener; - -import android.content.Context; -import android.view.GestureDetector; -import android.view.MotionEvent; -import android.view.View; - -import androidx.annotation.NonNull; -import androidx.recyclerview.widget.RecyclerView; - -public class SimpleRecyclerViewOnItemTouchListener implements RecyclerView.OnItemTouchListener { - - private GestureDetector mGestureDetector; - - private SimpleRecyclerViewOnItemTouchListener(Context context, Builder builder, RecyclerView recyclerView) { - MyGestureListener gestureListener = new MyGestureListener(recyclerView, - builder.mOnItemClickListener, - builder.mOnItemDoubleClickListener, - builder.mOnItemLongPressListener); - mGestureDetector = new GestureDetector(context, gestureListener); - } - - @Override - public boolean onInterceptTouchEvent(@NonNull RecyclerView recyclerView, - @NonNull MotionEvent motionEvent) { - mGestureDetector.onTouchEvent(motionEvent); - return false; - } - - @Override - public void onTouchEvent(@NonNull RecyclerView recyclerView, @NonNull MotionEvent motionEvent) {} - - @Override - public void onRequestDisallowInterceptTouchEvent(boolean b) {} - - public static class Builder { - - private SimpleRecyclerViewOnItemTouchListener.OnItemClickListener mOnItemClickListener = null; - private SimpleRecyclerViewOnItemTouchListener.OnItemDoubleClickListener mOnItemDoubleClickListener = null; - private SimpleRecyclerViewOnItemTouchListener.OnItemLongPressListener mOnItemLongPressListener = null; - - public Builder setOnItemClickListener(SimpleRecyclerViewOnItemTouchListener.OnItemClickListener mOnItemClickListener) { - this.mOnItemClickListener = mOnItemClickListener; - return this; - } - - public Builder setOnItemDoubleClickListener(SimpleRecyclerViewOnItemTouchListener.OnItemDoubleClickListener mOnItemDoubleClickListener) { - this.mOnItemDoubleClickListener = mOnItemDoubleClickListener; - return this; - } - - public Builder setOnItemLongPressListener(SimpleRecyclerViewOnItemTouchListener.OnItemLongPressListener mOnItemLongPressListener) { - this.mOnItemLongPressListener = mOnItemLongPressListener; - return this; - } - - public SimpleRecyclerViewOnItemTouchListener build(Context context, RecyclerView recyclerView) { - return new SimpleRecyclerViewOnItemTouchListener(context, this, recyclerView); - } - - } - - public interface OnItemClickListener { - - void onClick(View view, int position); - - } - - public interface OnItemDoubleClickListener { - - void onDoubleClick(View view, int position); - - } - - public interface OnItemLongPressListener { - - void onLongPress(View view, int position); - - } - -} diff --git a/simplerecyclerviewtouchlistener/src/main/java/io/github/horaciocome1/simplerecyclerviewtouchlistener/MyGestureListener.kt b/simplerecyclerviewtouchlistener/src/main/java/io/github/horaciocome1/simplerecyclerviewtouchlistener/MyGestureListener.kt new file mode 100644 index 0000000..0e5127a --- /dev/null +++ b/simplerecyclerviewtouchlistener/src/main/java/io/github/horaciocome1/simplerecyclerviewtouchlistener/MyGestureListener.kt @@ -0,0 +1,50 @@ +/* + * Copyright 2018 Horácio Flávio Comé Júnior + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific la + */ + +package io.github.horaciocome1.simplerecyclerviewtouchlistener + +import android.view.GestureDetector +import android.view.MotionEvent +import android.view.View +import androidx.recyclerview.widget.RecyclerView + +class MyGestureListener(val onClick: (View, Int) -> Unit, + val onDoubleClick: (View, Int) -> Unit, + val onLongPress: (View, Int) -> Unit, + private val recyclerView: RecyclerView) : GestureDetector.SimpleOnGestureListener() { + + override fun onSingleTapUp(e: MotionEvent?): Boolean { + if (e != null) { + val view = recyclerView.findChildViewUnder(e.x, e.y) + if (view != null) onClick(view, recyclerView.getChildAdapterPosition(view)) + } + return true + } + + override fun onDoubleTap(e: MotionEvent?): Boolean { + if (e != null) { + val view = recyclerView.findChildViewUnder(e.x, e.y) + if (view != null) onDoubleClick(view, recyclerView.getChildAdapterPosition(view)) + } + return true + } + + override fun onLongPress(e: MotionEvent?) { + if (e != null) { + val view = recyclerView.findChildViewUnder(e.x, e.y) + if (view != null) onLongPress(view, recyclerView.getChildAdapterPosition(view)) + } + } +} \ No newline at end of file diff --git a/simplerecyclerviewtouchlistener/src/main/java/io/github/horaciocome1/simplerecyclerviewtouchlistener/RecyclerViewUtils.kt b/simplerecyclerviewtouchlistener/src/main/java/io/github/horaciocome1/simplerecyclerviewtouchlistener/RecyclerViewUtils.kt new file mode 100644 index 0000000..dd69306 --- /dev/null +++ b/simplerecyclerviewtouchlistener/src/main/java/io/github/horaciocome1/simplerecyclerviewtouchlistener/RecyclerViewUtils.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2018 Horácio Flávio Comé Júnior + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific la + */ + +package io.github.horaciocome1.simplerecyclerviewtouchlistener + +import androidx.recyclerview.widget.RecyclerView + +fun RecyclerView.addSimpleTouchListener(touchListener: SimpleRecyclerViewOnItemTouchListener) { + touchListener.recyclerView = this + this.addOnItemTouchListener(touchListener) +} \ No newline at end of file diff --git a/simplerecyclerviewtouchlistener/src/main/java/io/github/horaciocome1/simplerecyclerviewtouchlistener/SimpleRecyclerViewOnItemTouchListener.kt b/simplerecyclerviewtouchlistener/src/main/java/io/github/horaciocome1/simplerecyclerviewtouchlistener/SimpleRecyclerViewOnItemTouchListener.kt new file mode 100644 index 0000000..ee5e47f --- /dev/null +++ b/simplerecyclerviewtouchlistener/src/main/java/io/github/horaciocome1/simplerecyclerviewtouchlistener/SimpleRecyclerViewOnItemTouchListener.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2018 Horácio Flávio Comé Júnior + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific la + */ + +package io.github.horaciocome1.simplerecyclerviewtouchlistener + +import android.view.GestureDetector +import android.view.MotionEvent +import android.view.View +import androidx.recyclerview.widget.RecyclerView + +class SimpleRecyclerViewOnItemTouchListener : RecyclerView.OnItemTouchListener { + + lateinit var recyclerView: RecyclerView + private var onClick = { _: View, _: Int -> Unit } + private var onDoubleClick = { _: View, _: Int -> Unit } + private var onLongPress = { _: View, _: Int -> Unit } + + override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) { + GestureDetector(recyclerView.context, MyGestureListener(onClick, onDoubleClick, onLongPress, recyclerView)) + } + + override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean { + return true + } + + override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) { + } + + fun setOnItemClickListener(onClick: (View, Int) -> Unit) { + this.onClick = onClick + } + + fun setOnItemDoubleClickListener(onDoubleClick: (View, Int) -> Unit) { + this.onDoubleClick = onDoubleClick + } + + fun setOnItemLongPressListener(onLongPress: (View, Int) -> Unit) { + this.onLongPress = onLongPress + } + + + +} \ No newline at end of file