Skip to content

Commit

Permalink
Merge pull request #11 from miguelbcr/features/#9-Add_onLongClickList…
Browse files Browse the repository at this point in the history
…ener_for_RecyclerView_items

#9-Add onLongClickListener for RecyclerView items
  • Loading branch information
miguelbcr committed Sep 8, 2016
2 parents ce1cb93 + 3a21e56 commit f755815
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 20 deletions.
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
compileSdkVersion 24
buildToolsVersion "24.0.1"

defaultConfig {
applicationId "miguelbcr.okadapters"
minSdkVersion 16
targetSdkVersion 23
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
Expand All @@ -22,8 +22,8 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:design:24.2.0'
compile "com.jakewharton:butterknife:7.0.1"
compile 'io.reactivex:rxjava:1.1.5'
compile 'io.reactivex:rxandroid:1.2.0'
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/app/recycler_view/RecyclerViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ private void setUpRecyclerView() {

adapter.setOnItemClickListener(new OkRecyclerViewAdapter.Listener<Item, ItemViewGroup>() {
@Override public void onClickItem(Item item, ItemViewGroup itemViewGroup, int position) {
Toast.makeText(RecyclerViewActivity.this, item.toString() + " " + " Pos: " + position, Toast.LENGTH_SHORT).show();
Toast.makeText(RecyclerViewActivity.this, item.toString() + " Pos: " + position, Toast.LENGTH_SHORT).show();
}
});

adapter.setOnItemLongClickListener(new OkRecyclerViewAdapter.ListenerLongClick<Item, ItemViewGroup>() {
@Override
public boolean onLongClickItem(Item item, ItemViewGroup itemViewGroup, int position) {
Toast.makeText(RecyclerViewActivity.this, item.toString() + " Pos: " + position + " (Long click)", Toast.LENGTH_SHORT).show();
return true;
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ private void setUpRecyclerView(boolean reverseLayout) {
adapter.setOnItemClickListener(new OkRecyclerViewAdapter.Listener<Item, ItemViewGroup>() {
@Override public void onClickItem(Item item, ItemViewGroup itemViewGroup, int position) {
startActivity(new Intent(RecyclerViewPagerActivity.this, DummyActivity.class));
Toast.makeText(RecyclerViewPagerActivity.this, item.toString() + " " + " Pos: " + position, Toast.LENGTH_SHORT).show();
Toast.makeText(RecyclerViewPagerActivity.this, item.toString() + " Pos: " + position, Toast.LENGTH_SHORT).show();
}
});

adapter.setOnItemLongClickListener(new OkRecyclerViewAdapter.ListenerLongClick<Item, ItemViewGroup>() {
@Override
public boolean onLongClickItem(Item item, ItemViewGroup itemViewGroup, int position) {
Toast.makeText(RecyclerViewPagerActivity.this, item.toString() + " Pos: " + position + " (Long click)", Toast.LENGTH_SHORT).show();
return true;
}
});

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Apr 08 13:36:45 CEST 2016
#Thu Sep 08 08:54:44 CEST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.11-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
5 changes: 2 additions & 3 deletions ok_adapters/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
apply plugin: 'com.android.library'

apply plugin: 'com.github.dcendents.android-maven'
group='com.github.miguelbcr'

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
buildToolsVersion "24.0.1"

defaultConfig {
minSdkVersion 16
Expand All @@ -23,7 +22,7 @@ android {

ext {
versions = [
support: "24.1.1"
support: "24.2.0"
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
*/
public abstract class OkRecyclerViewAdapter<T, V extends View & OkRecyclerViewAdapter.Binder<T>> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
protected List<T> items = new ArrayList<>();
protected Listener<T, V> listener;
private Listener<T, V> listener;
private ListenerLongClick<T, V> listenerLongClick;
private Pager<T, V> pager;
private final static int LOADING_VIEW_TYPE = 1, ITEM_VIEW_TYPE = 2;
boolean removeMoreListener;
Expand Down Expand Up @@ -63,11 +64,22 @@ public abstract class OkRecyclerViewAdapter<T, V extends View & OkRecyclerViewAd
final V view = viewHolder.getView();
view.bind(item, position, getItemCount());

if (listener != null) view.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
listener.onClickItem(item, view, viewHolder.getAdapterPosition());
}
});
if (listener != null) {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onClickItem(item, view, viewHolder.getAdapterPosition());
}
});
}

if (listenerLongClick != null) {
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override public boolean onLongClick(View v) {
return listenerLongClick.onLongClickItem(item, view, viewHolder.getAdapterPosition());
}
});
}
}

@Override public int getItemViewType(int position) {
Expand All @@ -83,6 +95,10 @@ public void setOnItemClickListener(Listener<T, V> listener) {
this.listener = listener;
}

public void setOnItemLongClickListener(ListenerLongClick<T, V> listener) {
this.listenerLongClick = listener;
}

public void add(T item) {
items.add(item);
notifyDataSetChanged();
Expand Down Expand Up @@ -111,6 +127,13 @@ public interface Listener<T, V> {
void onClickItem(T t, V v, int position);
}

public interface ListenerLongClick<T, V> {
/**
* @return true if the callback consumed the long click, false otherwise.
*/
boolean onLongClickItem(T t, V v, int position);
}

public interface Binder<T> {
void bind(T data, int position, int count);
}
Expand Down

0 comments on commit f755815

Please sign in to comment.