-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
124 additions
and
14 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
app/src/main/java/com/lai/mtc/comm/widget/MyPagerSnapHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.lai.mtc.comm.widget; | ||
|
||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.LinearSmoothScroller; | ||
import android.support.v7.widget.OrientationHelper; | ||
import android.support.v7.widget.PagerSnapHelper; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.util.DisplayMetrics; | ||
import android.view.View; | ||
|
||
public class MyPagerSnapHelper extends PagerSnapHelper { | ||
|
||
private RecyclerView mRecyclerView; | ||
private static final float MILLISECONDS_PER_INCH = 100f; | ||
private static final int MAX_SCROLL_ON_FLING_DURATION = 100; // ms | ||
|
||
private int mExtraSpace = 0; | ||
|
||
public MyPagerSnapHelper(int extraSpace) { | ||
mExtraSpace = extraSpace; | ||
} | ||
|
||
@Override | ||
public void attachToRecyclerView(@Nullable RecyclerView recyclerView) throws IllegalStateException { | ||
super.attachToRecyclerView(recyclerView); | ||
this.mRecyclerView = recyclerView; | ||
} | ||
|
||
@Override | ||
protected LinearSmoothScroller createSnapScroller(RecyclerView.LayoutManager layoutManager) { | ||
if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) { | ||
return null; | ||
} | ||
return new LinearSmoothScroller(mRecyclerView.getContext()) { | ||
@Override | ||
protected void onTargetFound(View targetView, RecyclerView.State state, RecyclerView.SmoothScroller.Action action) { | ||
int[] snapDistances = calculateDistanceToFinalSnap(mRecyclerView.getLayoutManager(), | ||
targetView); | ||
int dx = snapDistances[0]; | ||
final int dy = snapDistances[1]; | ||
if (dx < 0) { | ||
dx += mExtraSpace * mRecyclerView.getWidth(); | ||
if (dx > 0) { | ||
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager(); | ||
int firstItemPosition = linearLayoutManager.findFirstVisibleItemPosition(); | ||
if (firstItemPosition == 1) { | ||
dx = snapDistances[0] + mRecyclerView.getWidth(); | ||
} else { | ||
dx = -dx; | ||
} | ||
} | ||
} | ||
|
||
final int time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy))); | ||
if (time > 0) { | ||
action.update(dx, dy, time, mDecelerateInterpolator); | ||
} | ||
} | ||
|
||
@Override | ||
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { | ||
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi; | ||
} | ||
|
||
@Override | ||
protected int calculateTimeForScrolling(int dx) { | ||
return Math.min(MAX_SCROLL_ON_FLING_DURATION, super.calculateTimeForScrolling(dx)); | ||
} | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters