Last active
December 19, 2015 09:39
-
-
Save jgilfelt/5935050 to your computer and use it in GitHub Desktop.
DrawerLayout friendly ViewPager that will ignore rogue touch events from the bezel swipe gesture.
This file contains hidden or 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
/* | |
* Copyright (C) 2013 readyState Software Ltd | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.example; | |
import android.content.Context; | |
import android.support.v4.view.ViewPager; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
public class DrawerSafeViewPager extends ViewPager { | |
private static final int DEFAULT_GUTTER_SIZE = 16; // dips | |
private int mDefaultGutterSize; | |
private int mGutterSize; | |
public DrawerSafeViewPager(Context context) { | |
super(context); | |
init(); | |
} | |
public DrawerSafeViewPager(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
void init() { | |
final float density = getContext().getResources().getDisplayMetrics().density; | |
mDefaultGutterSize = (int) (DEFAULT_GUTTER_SIZE * density); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
final int measuredWidth = getMeasuredWidth(); | |
final int maxGutterSize = measuredWidth / 10; | |
mGutterSize = Math.min(maxGutterSize, mDefaultGutterSize); | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent ev) { | |
// Don't handle events within the left gutter. This fixes an issue with | |
// ViewPager briefly intercepting the bezel swipe gesture within a DrawerLayout | |
// and starting a drag movement, causing a visual stutter. | |
if (ev.getAction() != MotionEvent.ACTION_UP && ev.getX() < mGutterSize) { | |
return false; | |
} | |
return super.onTouchEvent(ev); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment