Skip to content

Commit

Permalink
Set the position of the view holder as a tag for easy retrieval to em…
Browse files Browse the repository at this point in the history
…ulate getAdapterPosition in ViewHolder
  • Loading branch information
Jawnnypoo committed Oct 28, 2016
1 parent 77f0737 commit 45f6a0d
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 34 deletions.
8 changes: 4 additions & 4 deletions adapterflowlayout/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
compileSdkVersion 25
buildToolsVersion "25.0.0"

defaultConfig {
minSdkVersion 9
targetSdkVersion 24
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Expand Down
10 changes: 5 additions & 5 deletions adapterlayout/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
compileSdkVersion 25
buildToolsVersion "25.0.0"

defaultConfig {
minSdkVersion 9
targetSdkVersion 24
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Expand All @@ -23,7 +23,7 @@ android {

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

apply from: 'https://raw.githubusercontent.com/Commit451/gradle-android-javadocs/1.0.0/gradle-android-javadocs.gradle'
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.commit451.adapterlayout;

import android.support.v7.widget.RecyclerView;

/**
* Provides access to some of the public API of AdapterLayout in a way that will be available for any given
* AdapterLayout
*/
public class AdapterLayout {

/**
* Get the position in the AdapterLayout of the View holder.
* Use this instead of {@link RecyclerView.ViewHolder#getAdapterPosition()}
*
* @param viewHolder the holder to find the position of
* @return the index of the holder, or -1 if not found
*/
public static int getAdapterPosition(RecyclerView.ViewHolder viewHolder) {
return getPosition(viewHolder);
}

/**
* Get the position in the AdapterLayout of the View holder.
* Use this instead of {@link RecyclerView.ViewHolder#getLayoutPosition()}
*
* @param viewHolder the holder to find the position of
* @return the index of the holder, or -1 if not found
*/
public static int getLayoutPosition(RecyclerView.ViewHolder viewHolder) {
return getPosition(viewHolder);
}

private static int getPosition(RecyclerView.ViewHolder viewHolder) {
return (int) viewHolder.itemView.getTag(R.id.adapter_layout_list_position);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public void setAdapter(@Nullable RecyclerView.Adapter adapter) {
*
* @return the adapter
*/
@Nullable
public RecyclerView.Adapter getAdapter() {
return mAdapter;
}
Expand Down Expand Up @@ -125,8 +124,10 @@ private void addViewAt(int index) {

private void addViewAt(int viewType, int index) {
RecyclerView.ViewHolder viewHolder = mAdapter.onCreateViewHolder(mViewGroup, viewType);
//setting the lib to min 4.0 to avoid leaks from doing this
viewHolder.itemView.setTag(R.id.adapter_layout_list_holder, viewHolder);
viewHolder.itemView.setTag(R.id.adapter_layout_list_view_type, viewType);
viewHolder.itemView.setTag(R.id.adapter_layout_list_position, index);
mViewGroup.addView(viewHolder.itemView);
mAdapter.onBindViewHolder(viewHolder, index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public void setAdapter(RecyclerView.Adapter adapter) {
mAdapterLayoutDelegate.setAdapter(adapter);
}

@Nullable
public RecyclerView.Adapter getAdapter() {
if (mAdapterLayoutDelegate != null) {
return mAdapterLayoutDelegate.getAdapter();
Expand Down
1 change: 1 addition & 0 deletions adapterlayout/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<resources>
<item name="adapter_layout_list_holder" type="id" />
<item name="adapter_layout_list_view_type" type="id"/>
<item name="adapter_layout_list_position" type="id"/>
</resources>
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 24
buildToolsVersion "24.0.2"
compileSdkVersion 25
buildToolsVersion "25.0.0"

defaultConfig {
applicationId "com.commit451.adapterlayout.sample"
minSdkVersion 15
targetSdkVersion 24
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
Expand All @@ -25,8 +25,8 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile project(':adapterlayout')
compile project(':adapterflowlayout')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.view.View;
import android.view.ViewGroup;

import com.commit451.adapterlayout.AdapterLayout;

import java.util.ArrayList;
import java.util.Collection;

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

private Listener mListener;
private ArrayList<Cheese> mValues;
private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (int) v.getTag(R.id.list_position);
Cheese cheese = getItemAt(position);
mListener.onItemClicked(cheese);
}
};

public CheeseAdapter(Listener listener) {
mListener = listener;
Expand Down Expand Up @@ -73,17 +67,23 @@ public void clear() {

@Override
public CheeseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CheeseViewHolder holder = CheeseViewHolder.inflate(parent);
holder.itemView.setOnClickListener(mOnClickListener);
final CheeseViewHolder holder = CheeseViewHolder.inflate(parent);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Takes the place of holder.getAdapterPosition()
int position = AdapterLayout.getAdapterPosition(holder);
Cheese cheese = getItemAt(position);
mListener.onItemClicked(cheese);
}
});
return holder;
}

@Override
public void onBindViewHolder(final CheeseViewHolder holder, int position) {
Cheese cheese = getItemAt(position);
holder.bind(cheese);
holder.itemView.setTag(R.id.list_position, position);
holder.itemView.setTag(R.id.list_holder, holder);
}

@Override
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/res/values/ids.xml

This file was deleted.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.github.ben-manes:gradle-versions-plugin:0.13.0'

Expand Down

0 comments on commit 45f6a0d

Please sign in to comment.