Skip to content

Commit

Permalink
add CardLinearSnapHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
huachangjian committed Sep 3, 2016
2 parents 8f3ae6b + c52174f commit 75311b4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.view.jameson.library;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
* 防止卡片在第一页和最后一页因无法"居中"而一直循环调用onScrollStateChanged-->SnapHelper.snapToTargetExistingView-->onScrollStateChanged
* Created by jameson on 9/3/16.
*/
public class CardLinearSnapHelper extends LinearSnapHelper {
public boolean mNoNeedToScroll = false;

@Override
public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView) {
if (mNoNeedToScroll) {
return new int[]{0, 0};
} else {
return super.calculateDistanceToFinalSnap(layoutManager, targetView);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.view.jameson.library;

import android.content.Context;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import jameson.io.library.util.LogUtils;
Expand All @@ -26,7 +26,9 @@ public class CardScaleHelper {
private int mCurrentItemPos;
private int mCurrentItemOffset;

public void attachToRecyclerView(RecyclerView mRecyclerView) {
private CardLinearSnapHelper mLinearSnapHelper = new CardLinearSnapHelper();

public void attachToRecyclerView(final RecyclerView mRecyclerView) {
// 开启log会影响滑动体验, 调试时才开启
LogUtils.mLogEnable = false;
this.mRecyclerView = mRecyclerView;
Expand All @@ -35,6 +37,11 @@ public void attachToRecyclerView(RecyclerView mRecyclerView) {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
mLinearSnapHelper.mNoNeedToScroll = mCurrentItemOffset == 0 || mCurrentItemOffset == getDestItemOffset(mRecyclerView.getAdapter().getItemCount() - 1);
} else {
mLinearSnapHelper.mNoNeedToScroll = false;
}
}

@Override
Expand All @@ -49,7 +56,7 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
});

initWidth();
new LinearSnapHelper().attachToRecyclerView(mRecyclerView);
mLinearSnapHelper.attachToRecyclerView(mRecyclerView);
}

/**
Expand All @@ -62,7 +69,6 @@ public void run() {
mCardGalleryWidth = mRecyclerView.getWidth();
mCardWidth = mCardGalleryWidth - ScreenUtil.dip2px(mContext, 2 * (mPagePadding + mShowLeftCardWidth));
mOnePageWidth = mCardWidth;
mCurrentItemOffset = getDestItemOffset(mCurrentItemPos);
mRecyclerView.smoothScrollToPosition(mCurrentItemPos);
onScrolledChangedCallback();
}
Expand Down

0 comments on commit 75311b4

Please sign in to comment.