Skip to content

Instantly share code, notes, and snippets.

@denisviana
Last active December 16, 2020 11:30
Show Gist options
  • Save denisviana/5277e1420bef9a180fdbc1cc30ba3baf to your computer and use it in GitHub Desktop.
Save denisviana/5277e1420bef9a180fdbc1cc30ba3baf to your computer and use it in GitHub Desktop.
ViewPager with option to enable/disable swipe
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Declare custom theme attributes that allow changing which styles are
used for button bars depending on the API level.
?android:attr/buttonBarStyle is new as of API 11 so this is
necessary to support previous API levels. -->
<declare-styleable name="ButtonBarContainerTheme">
<attr name="metaButtonBarStyle" format="reference" />
<attr name="metaButtonBarButtonStyle" format="reference" />
</declare-styleable>
<declare-styleable name="CustomViewPager">
<attr name="swipeable" format="boolean" />
</declare-styleable>
</resources>
package br.net.ouroverde.agendamentoleves.utils;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import br.net.ouroverde.agendamentoleves.R;
/**
* Created by Denis Costa on 17/11/2017.
*/
public class CustomViewPager extends ViewPager {
private boolean swipeable;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.swipeable = true;
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomViewPager);
try {
swipeable = a.getBoolean(R.styleable.CustomViewPager_swipeable, true);
} finally {
a.recycle();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.swipeable) {
return super.onTouchEvent(event);
}
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.swipeable) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.swipeable = enabled;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment