Skip to content

Commit

Permalink
Merge pull request #206 from mateusz0matti/master
Browse files Browse the repository at this point in the history
Support rtl
  • Loading branch information
lsjwzh committed Mar 21, 2018
2 parents 951bb71 + b6d2e86 commit 0df8747
Showing 1 changed file with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.os.Build;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v4.text.TextUtilsCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
Expand All @@ -20,6 +22,7 @@
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
* RecyclerViewPager
Expand Down Expand Up @@ -322,11 +325,18 @@ public int getCurrentPosition() {
return curPosition;
}


private boolean isLeftToRightMode(){
return TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_LTR;
}

/***
* adjust position before Touch event complete and fling action start.
*/
protected void adjustPositionX(int velocityX) {

if (reverseLayout) velocityX *= -1;
if (!isLeftToRightMode()) velocityX *= -1;

int childCount = getChildCount();
if (childCount > 0) {
Expand Down Expand Up @@ -464,8 +474,8 @@ public boolean onInterceptTouchEvent(MotionEvent e) {
touchStartPoint.set(x, y);
break;
case MotionEvent.ACTION_MOVE:
float tempDistance = (float) Math.sqrt(x*x+ y*y);
float lastDistance = (float) Math.sqrt(touchStartPoint.x*touchStartPoint.x + touchStartPoint.y*touchStartPoint.y);
float tempDistance = (float) Math.sqrt(x * x + y * y);
float lastDistance = (float) Math.sqrt(touchStartPoint.x * touchStartPoint.x + touchStartPoint.y * touchStartPoint.y);

if (Math.abs(lastDistance - tempDistance) > minSlideDistance) {
float k = Math.abs((touchStartPoint.y - y) / (touchStartPoint.x - x));
Expand Down Expand Up @@ -523,14 +533,22 @@ public void onScrollStateChanged(int state) {
if (mCurView != null) {
targetPosition = getChildAdapterPosition(mCurView);
if (getLayoutManager().canScrollHorizontally()) {
boolean leftToRight = isLeftToRightMode();
int spanX = mCurView.getLeft() - mFisrtLeftWhenDragging;
// if user is tending to cancel paging action, don't perform position changing
if (spanX > mCurView.getWidth() * mTriggerOffset && mCurView.getLeft() >= mMaxLeftWhenDragging) {
if (!reverseLayout) targetPosition--;
else targetPosition++;
if (!reverseLayout) {
targetPosition = leftToRight ? (targetPosition - 1) : (targetPosition + 1);
}
else {
targetPosition = leftToRight ? (targetPosition + 1) : (targetPosition - 1);
}
} else if (spanX < mCurView.getWidth() * -mTriggerOffset && mCurView.getLeft() <= mMinLeftWhenDragging) {
if (!reverseLayout) targetPosition++;
else targetPosition--;
if (!reverseLayout) {
targetPosition = leftToRight ? (targetPosition + 1) : (targetPosition - 1);
}
else {
targetPosition = leftToRight ? (targetPosition - 1) : (targetPosition + 1);
}
}
} else {
int spanY = mCurView.getTop() - mFirstTopWhenDragging;
Expand Down Expand Up @@ -590,6 +608,7 @@ private int safeTargetPosition(int position, int count) {
public interface OnPageChangedListener {
/**
* Fires when viewpager changes it's page
*
* @param oldPosition old position
* @param newPosition new position
*/
Expand Down

0 comments on commit 0df8747

Please sign in to comment.