Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for horizontal scrolling content #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,55 @@
*/

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;

import me.kaelaela.verticalviewpager.transforms.DefaultTransformer;

public class VerticalViewPager extends ViewPager {
// Intercept threshold default (in dp)
private static final float DEFAULT_THRESHOLD_Y = 16f;

// Variables used for intercepting touches
private float startY;
private float thresholdY;

public VerticalViewPager(Context context) {
this(context, null);
super(context);
init(context, null);
}

public VerticalViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
setPageTransformer(false, new DefaultTransformer());
init(context, attrs);
}

private MotionEvent swapTouchEvent(MotionEvent event) {
float width = getWidth();
float height = getHeight();

float swappedX = (event.getY() / height) * width;
float swappedY = (event.getX() / width) * height;
private void init(Context context, AttributeSet attrs) {
setPageTransformer(false, new DefaultTransformer());

event.setLocation(swappedX, swappedY);
if (attrs == null) {
thresholdY = convertDpToPixel(context, DEFAULT_THRESHOLD_Y);
return;
}

return event;
}
TypedArray attributes = null;
try {
attributes = context.obtainStyledAttributes(attrs, R.styleable.VerticalViewPager);
thresholdY = attributes.getDimensionPixelSize(R.styleable.VerticalViewPager_vvp_threshold, convertDpToPixel(context, DEFAULT_THRESHOLD_Y));
} finally {
if (attributes != null) {
attributes.recycle();
}
}
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
boolean intercept = super.onInterceptTouchEvent(swapTouchEvent(event));
boolean intercept = shouldIntercept(event) || super.onInterceptTouchEvent(swapTouchEvent(event));
//If not intercept, touch event should not be swapped.
swapTouchEvent(event);
return intercept;
Expand All @@ -56,4 +74,34 @@ public boolean onTouchEvent(MotionEvent ev) {
return super.onTouchEvent(swapTouchEvent(ev));
}

private boolean shouldIntercept(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
startY = event.getY();
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (Math.abs(startY - event.getY()) > thresholdY) {
return true;
}
}

return false;
}

private MotionEvent swapTouchEvent(MotionEvent event) {
float width = getWidth() - getPaddingLeft() - getPaddingRight();
float height = getHeight() - getPaddingTop() - getPaddingBottom();

float swappedX = (event.getY() / height) * width;
float swappedY = (event.getX() / width) * height;

event.setLocation(swappedX, swappedY);

return event;
}

private static int convertDpToPixel(Context context, float dp) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();

return (int) (dp * (metrics.densityDpi / 160f));
}
}
8 changes: 8 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="VerticalViewPager">
<attr name="vvp_threshold" format="dimension"/>
</declare-styleable>

</resources>