Skip to content

Commit

Permalink
fix bug:loopviewpager position confusion
Browse files Browse the repository at this point in the history
  • Loading branch information
lsjwzh committed Apr 30, 2016
1 parent cac9a40 commit d8c8205
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':lib')
compile project(':tablayoutsupport')
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'

compile 'com.github.bumptech.glide:glide:3.6.0'
compile 'de.hdodenhof:circleimageview:1.3.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.Log;

public class LoopRecyclerViewPager extends RecyclerViewPager {

Expand Down Expand Up @@ -47,7 +48,9 @@ protected RecyclerViewPagerAdapter ensureRecyclerViewPagerAdapter(Adapter adapte
*/
@Override
public void smoothScrollToPosition(int position) {
super.smoothScrollToPosition(transformInnerPositionIfNeed(position));
int transformedPosition = transformInnerPositionIfNeed(position);
super.smoothScrollToPosition(transformedPosition);
Log.e("test", "transformedPosition:" + transformedPosition);
}

/**
Expand Down Expand Up @@ -76,15 +79,15 @@ public int getActualCurrentPosition() {
* @return actual position
*/
public int transformToActualPosition(int position) {
return position % getActualItemCountFromAdpater();
return position % getActualItemCountFromAdapter();
}

private int getActualItemCountFromAdpater() {
private int getActualItemCountFromAdapter() {
return ((LoopRecyclerViewPagerAdapter) getWrapperAdapter()).getActualItemCount();
}

private int transformInnerPositionIfNeed(int position) {
final int actualItemCount = getActualItemCountFromAdpater();
final int actualItemCount = getActualItemCountFromAdapter();
final int actualCurrentPosition = getCurrentPosition() % actualItemCount;
int bakPosition1 = getCurrentPosition()
- actualCurrentPosition
Expand All @@ -97,6 +100,7 @@ private int transformInnerPositionIfNeed(int position) {
- actualCurrentPosition
+ actualItemCount
+ position % actualItemCount;
Log.e("test", bakPosition1 + "/" + bakPosition2 + "/" + bakPosition3 + "/" + getCurrentPosition());
// get position which is closer to current position
if (Math.abs(bakPosition1 - getCurrentPosition()) > Math.abs(bakPosition2 -
getCurrentPosition())){
Expand All @@ -118,7 +122,7 @@ private int transformInnerPositionIfNeed(int position) {

private int getMiddlePosition() {
int middlePosition = Integer.MAX_VALUE / 2;
final int actualItemCount = getActualItemCountFromAdpater();
final int actualItemCount = getActualItemCountFromAdapter();
if (actualItemCount > 0 && middlePosition % actualItemCount != 0) {
middlePosition = middlePosition - middlePosition % actualItemCount;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import android.support.v7.widget.RecyclerView;

import java.lang.reflect.Field;

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

private Field mPositionField;

public LoopRecyclerViewPagerAdapter(RecyclerViewPager viewPager, RecyclerView.Adapter<VH> adapter) {
super(viewPager, adapter);
}
Expand Down Expand Up @@ -39,6 +43,21 @@ public long getItemId(int position) {
@Override
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 (mPositionField == null) {
try {
mPositionField = holder.getClass().getDeclaredField("mPosition");
mPositionField.setAccessible(true);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
try {
mPositionField.set(holder, position);;
} catch (Exception e) {
e.printStackTrace();
}
}

public int getActualPosition(int position) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
final float positionOffset = offset * 1f / pagerWidth;
if (tabLayout != null) {
if (positionOffset < 0) {
tabLayout.setScrollPosition(mPositionBeforeScroll
+ (int) Math.floor(positionOffset),
try {
tabLayout.setScrollPosition(mPositionBeforeScroll
+ (int) Math.floor(positionOffset),
positionOffset - (int) Math.floor(positionOffset),
false);
} catch (Throwable e) {
e.printStackTrace();
}
} else {
tabLayout.setScrollPosition(mPositionBeforeScroll + (int) (positionOffset),
positionOffset - (int) (positionOffset),
Expand Down

0 comments on commit d8c8205

Please sign in to comment.