Skip to content

Commit

Permalink
Merge pull request #125 from esmasui/master
Browse files Browse the repository at this point in the history
Fixed to set mPosition to ViewHolder
  • Loading branch information
lsjwzh authored Nov 9, 2016
2 parents 71b7108 + 45b64b8 commit 2b025af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package android.support.v7.widget;

public abstract class ViewHolderDelegate {

private ViewHolderDelegate() {
throw new UnsupportedOperationException("no instances");
}

public static void setPosition(RecyclerView.ViewHolder viewHolder, int position) {
viewHolder.mPosition = position;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package com.lsjwzh.widget.recyclerviewpager;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.support.v7.widget.ViewHolderDelegate;

import java.lang.reflect.Field;

public class LoopRecyclerViewPagerAdapter<VH extends RecyclerView.ViewHolder>
extends RecyclerViewPagerAdapter<VH> {

private static final String TAG = LoopRecyclerViewPager.class.getSimpleName();

private Field mPositionField;

public LoopRecyclerViewPagerAdapter(RecyclerViewPager viewPager, RecyclerView.Adapter<VH> adapter) {
super(viewPager, adapter);
}
Expand Down Expand Up @@ -48,21 +43,7 @@ public void onBindViewHolder(VH holder, int position) {
super.onBindViewHolder(holder, getActualPosition(position));
// because of getCurrentPosition may return ViewHolder‘s position,
// so we must reset mPosition if exists.
if (mPositionField == null) {
try {
mPositionField = holder.getClass().getDeclaredField("mPosition");
mPositionField.setAccessible(true);
} catch (NoSuchFieldException e) {
Log.i(TAG, "The holder doesn't have a mPosition field.");
}
}
if (mPositionField != null) {
try {
mPositionField.set(holder, position);
} catch (Exception e) {
Log.w(TAG, "Error while updating holder's mPosition field", e);
}
}
ViewHolderDelegate.setPosition(holder, position);
}

public int getActualPosition(int position) {
Expand Down

0 comments on commit 2b025af

Please sign in to comment.