From d8c8205bb04e3dc83d83024e94b20626d77ae012 Mon Sep 17 00:00:00 2001 From: lsjwzh Date: Sat, 30 Apr 2016 10:06:14 +0800 Subject: [PATCH] fix bug:loopviewpager position confusion --- app/build.gradle | 4 ++-- .../LoopRecyclerViewPager.java | 14 +++++++++----- .../LoopRecyclerViewPagerAdapter.java | 19 +++++++++++++++++++ .../recyclerviewpager/TabLayoutSupport.java | 8 ++++++-- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d58fde2..fbb0a52 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/lib/src/main/java/com/lsjwzh/widget/recyclerviewpager/LoopRecyclerViewPager.java b/lib/src/main/java/com/lsjwzh/widget/recyclerviewpager/LoopRecyclerViewPager.java index f6b64b9..d0b6485 100644 --- a/lib/src/main/java/com/lsjwzh/widget/recyclerviewpager/LoopRecyclerViewPager.java +++ b/lib/src/main/java/com/lsjwzh/widget/recyclerviewpager/LoopRecyclerViewPager.java @@ -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 { @@ -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); } /** @@ -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 @@ -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())){ @@ -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; } diff --git a/lib/src/main/java/com/lsjwzh/widget/recyclerviewpager/LoopRecyclerViewPagerAdapter.java b/lib/src/main/java/com/lsjwzh/widget/recyclerviewpager/LoopRecyclerViewPagerAdapter.java index 24a95cf..df633a4 100644 --- a/lib/src/main/java/com/lsjwzh/widget/recyclerviewpager/LoopRecyclerViewPagerAdapter.java +++ b/lib/src/main/java/com/lsjwzh/widget/recyclerviewpager/LoopRecyclerViewPagerAdapter.java @@ -2,9 +2,13 @@ import android.support.v7.widget.RecyclerView; +import java.lang.reflect.Field; + public class LoopRecyclerViewPagerAdapter extends RecyclerViewPagerAdapter { + private Field mPositionField; + public LoopRecyclerViewPagerAdapter(RecyclerViewPager viewPager, RecyclerView.Adapter adapter) { super(viewPager, adapter); } @@ -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) { diff --git a/tablayoutsupport/src/main/java/com/lsjwzh/widget/recyclerviewpager/TabLayoutSupport.java b/tablayoutsupport/src/main/java/com/lsjwzh/widget/recyclerviewpager/TabLayoutSupport.java index 69b84b4..6b2039e 100644 --- a/tablayoutsupport/src/main/java/com/lsjwzh/widget/recyclerviewpager/TabLayoutSupport.java +++ b/tablayoutsupport/src/main/java/com/lsjwzh/widget/recyclerviewpager/TabLayoutSupport.java @@ -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),