Skip to content

Commit 45f6a0d

Browse files
committed
Set the position of the view holder as a tag for easy retrieval to emulate getAdapterPosition in ViewHolder
1 parent 77f0737 commit 45f6a0d

File tree

10 files changed

+66
-34
lines changed

10 files changed

+66
-34
lines changed

adapterflowlayout/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 24
5-
buildToolsVersion "24.0.2"
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.0"
66

77
defaultConfig {
8-
minSdkVersion 9
9-
targetSdkVersion 24
8+
minSdkVersion 15
9+
targetSdkVersion 25
1010
versionCode 1
1111
versionName "1.0"
1212
}

adapterlayout/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
apply plugin: 'com.android.library'
22

33
android {
4-
compileSdkVersion 24
5-
buildToolsVersion "24.0.2"
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.0"
66

77
defaultConfig {
8-
minSdkVersion 9
9-
targetSdkVersion 24
8+
minSdkVersion 15
9+
targetSdkVersion 25
1010
versionCode 1
1111
versionName "1.0"
1212
}
@@ -23,7 +23,7 @@ android {
2323

2424
dependencies {
2525
compile fileTree(include: ['*.jar'], dir: 'libs')
26-
compile 'com.android.support:recyclerview-v7:24.2.1'
26+
compile 'com.android.support:recyclerview-v7:25.0.0'
2727
}
2828

2929
apply from: 'https://raw.githubusercontent.com/Commit451/gradle-android-javadocs/1.0.0/gradle-android-javadocs.gradle'
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.commit451.adapterlayout;
2+
3+
import android.support.v7.widget.RecyclerView;
4+
5+
/**
6+
* Provides access to some of the public API of AdapterLayout in a way that will be available for any given
7+
* AdapterLayout
8+
*/
9+
public class AdapterLayout {
10+
11+
/**
12+
* Get the position in the AdapterLayout of the View holder.
13+
* Use this instead of {@link RecyclerView.ViewHolder#getAdapterPosition()}
14+
*
15+
* @param viewHolder the holder to find the position of
16+
* @return the index of the holder, or -1 if not found
17+
*/
18+
public static int getAdapterPosition(RecyclerView.ViewHolder viewHolder) {
19+
return getPosition(viewHolder);
20+
}
21+
22+
/**
23+
* Get the position in the AdapterLayout of the View holder.
24+
* Use this instead of {@link RecyclerView.ViewHolder#getLayoutPosition()}
25+
*
26+
* @param viewHolder the holder to find the position of
27+
* @return the index of the holder, or -1 if not found
28+
*/
29+
public static int getLayoutPosition(RecyclerView.ViewHolder viewHolder) {
30+
return getPosition(viewHolder);
31+
}
32+
33+
private static int getPosition(RecyclerView.ViewHolder viewHolder) {
34+
return (int) viewHolder.itemView.getTag(R.id.adapter_layout_list_position);
35+
}
36+
}

adapterlayout/src/main/java/com/commit451/adapterlayout/AdapterLayoutDelegate.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ public void setAdapter(@Nullable RecyclerView.Adapter adapter) {
9292
*
9393
* @return the adapter
9494
*/
95-
@Nullable
9695
public RecyclerView.Adapter getAdapter() {
9796
return mAdapter;
9897
}
@@ -125,8 +124,10 @@ private void addViewAt(int index) {
125124

126125
private void addViewAt(int viewType, int index) {
127126
RecyclerView.ViewHolder viewHolder = mAdapter.onCreateViewHolder(mViewGroup, viewType);
127+
//setting the lib to min 4.0 to avoid leaks from doing this
128128
viewHolder.itemView.setTag(R.id.adapter_layout_list_holder, viewHolder);
129129
viewHolder.itemView.setTag(R.id.adapter_layout_list_view_type, viewType);
130+
viewHolder.itemView.setTag(R.id.adapter_layout_list_position, index);
130131
mViewGroup.addView(viewHolder.itemView);
131132
mAdapter.onBindViewHolder(viewHolder, index);
132133
}

adapterlayout/src/main/java/com/commit451/adapterlayout/AdapterLinearLayout.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public void setAdapter(RecyclerView.Adapter adapter) {
4343
mAdapterLayoutDelegate.setAdapter(adapter);
4444
}
4545

46-
@Nullable
4746
public RecyclerView.Adapter getAdapter() {
4847
if (mAdapterLayoutDelegate != null) {
4948
return mAdapterLayoutDelegate.getAdapter();

adapterlayout/src/main/res/values/ids.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
<resources>
33
<item name="adapter_layout_list_holder" type="id" />
44
<item name="adapter_layout_list_view_type" type="id"/>
5+
<item name="adapter_layout_list_position" type="id"/>
56
</resources>

app/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 24
5-
buildToolsVersion "24.0.2"
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.0"
66

77
defaultConfig {
88
applicationId "com.commit451.adapterlayout.sample"
99
minSdkVersion 15
10-
targetSdkVersion 24
10+
targetSdkVersion 25
1111
versionCode 1
1212
versionName "1.0"
1313
}
@@ -25,8 +25,8 @@ android {
2525
dependencies {
2626
compile fileTree(dir: 'libs', include: ['*.jar'])
2727
testCompile 'junit:junit:4.12'
28-
compile 'com.android.support:appcompat-v7:24.2.1'
29-
compile 'com.android.support:design:24.2.1'
28+
compile 'com.android.support:appcompat-v7:25.0.0'
29+
compile 'com.android.support:design:25.0.0'
3030
compile 'com.jakewharton:butterknife:7.0.1'
3131
compile project(':adapterlayout')
3232
compile project(':adapterflowlayout')

app/src/main/java/com/commit451/adapterlayout/sample/CheeseAdapter.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import android.view.View;
55
import android.view.ViewGroup;
66

7+
import com.commit451.adapterlayout.AdapterLayout;
8+
79
import java.util.ArrayList;
810
import java.util.Collection;
911

@@ -14,14 +16,6 @@ public class CheeseAdapter extends RecyclerView.Adapter<CheeseViewHolder> {
1416

1517
private Listener mListener;
1618
private ArrayList<Cheese> mValues;
17-
private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
18-
@Override
19-
public void onClick(View v) {
20-
int position = (int) v.getTag(R.id.list_position);
21-
Cheese cheese = getItemAt(position);
22-
mListener.onItemClicked(cheese);
23-
}
24-
};
2519

2620
public CheeseAdapter(Listener listener) {
2721
mListener = listener;
@@ -73,17 +67,23 @@ public void clear() {
7367

7468
@Override
7569
public CheeseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
76-
CheeseViewHolder holder = CheeseViewHolder.inflate(parent);
77-
holder.itemView.setOnClickListener(mOnClickListener);
70+
final CheeseViewHolder holder = CheeseViewHolder.inflate(parent);
71+
holder.itemView.setOnClickListener(new View.OnClickListener() {
72+
@Override
73+
public void onClick(View v) {
74+
//Takes the place of holder.getAdapterPosition()
75+
int position = AdapterLayout.getAdapterPosition(holder);
76+
Cheese cheese = getItemAt(position);
77+
mListener.onItemClicked(cheese);
78+
}
79+
});
7880
return holder;
7981
}
8082

8183
@Override
8284
public void onBindViewHolder(final CheeseViewHolder holder, int position) {
8385
Cheese cheese = getItemAt(position);
8486
holder.bind(cheese);
85-
holder.itemView.setTag(R.id.list_position, position);
86-
holder.itemView.setTag(R.id.list_holder, holder);
8787
}
8888

8989
@Override

app/src/main/res/values/ids.xml

Lines changed: 0 additions & 5 deletions
This file was deleted.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.2.0'
8+
classpath 'com.android.tools.build:gradle:2.2.2'
99
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
1010
classpath 'com.github.ben-manes:gradle-versions-plugin:0.13.0'
1111

0 commit comments

Comments
 (0)