otak info official Logo   
OTAK.INFO
Personal Blog

Hariyanto Lim

정길상 / 鄭吉祥
Home   Last Posts  
 
Please login to see more topics and discussion categories.
 
goto main category list >> Software programming >> Android

Title : Disable ViewPager swipe to other page if touching on HorizontalScrollView
Total Reply : 0
Total View : 3180


Harry
Harry
Total thread: 131
Total reply: 50

Post #153
Disable ViewPager swipe to other page if touching on HorizontalScrollView
In most all scenarios, we need to improve user interaction with our UI app by providing a better and a CONSTANT "feel" operation to our app.

Android by default when we have HorizontalScrollView inside ViewPager, sometimes when user do a swipe gesture inside HorizontalScrollView to scroll through all items inside HorizontalScrollView, the Android UI framework will let VIewPager (parent of HorizontalScrollView) to switch to other page which we feel not as what we want.

So if we want to take the WHOLE touch gesture for HorizontalScrollView and do not let ViewPager to handle then we need to do some customisation.

The logic explain below is simple : "Do not let ViewPager to take and process the touch gesture when the touch is inside HorizontalScrollView, so we need to detect whether HorizontalScrollView is touching or not, then we will tell ViewPager about it so ViewPager can understand whether to do a switch to other page or not.

First, we need to create a custom view pager (MyCustomViewPager.java)
'
'.str_replace('
', ' ', '
public class MyCustomViewPager extends android.support.v4.view.ViewPager implements AllowViewPagerSwipeInterface{

private final String TAG = MyCustomViewPager.class.getSimpleName();

public MyCustomViewPager(Context context) {
this(context, null);
}
public MyCustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
Log.d(TAG, "onInterceptTouchEvent(MotionEvent), allow swipe to switch page: " + bAllowSwipeToSwitchPage);
// // Never allow swiping to switch between pages
// return false;

return bAllowSwipeToSwitchPage ? super.onInterceptTouchEvent(event) : false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "onTouchEvent(MotionEvent), allow swipe to switch page: " + bAllowSwipeToSwitchPage);

// // Never allow swiping to switch between pages
// return false;

return bAllowSwipeToSwitchPage ? super.onTouchEvent(event) : false;
}

// default is ALLOW SWIPE
private boolean bAllowSwipeToSwitchPage = true;

@Override
public void setAllowSwipeViewPager(boolean allow) {
Log.d(TAG, "setAllowSwipeViewPager(allow: " + allow + ")");
bAllowSwipeToSwitchPage = allow;
}
}
').'
'

Custom interface (AllowViewPagerSwipeInterface.java)
'
'.str_replace('
', ' ', '
public interface AllowViewPagerSwipeInterface {
public void setAllowSwipeViewPager(boolean allow);
}
').'
'

Custom HorizontalScrollView (HInterceptHorizontalScrollView.java)
'
'.str_replace('
', ' ', '
public class HInterceptHorizontalScrollView extends HorizontalScrollView {

private final static String TAG = HInterceptHorizontalScrollView.class.getSimpleName();

public HInterceptHorizontalScrollView(Context context) {
this(context, null);
}
public HInterceptHorizontalScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public HInterceptHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

if(isInEditMode()) {return;}
}

public void allowSwipeViewPager(boolean allow) {
Log.d(TAG, "allowSwipeViewPager(allow: " + allow + ")");

if(allow == false) {

// detect touch event
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

int action = event.getAction();
switch(action) {
case MotionEvent.ACTION_DOWN: {
isTouching = true;

Log.d(TAG, "start touching");
} break;
case MotionEvent.ACTION_MOVE: {
isTouching = true;

Log.d(TAG, "moving");
} break;
default: {
isTouching = false;

Log.d(TAG, "other action: " + action);
} break;
}

// For simplicity, we make MainActivity.mViewPager as public static == global
if(MainActivity.mViewPager != null) {
MainActivity.mViewPager.setAllowSwipeViewPager( ! isTouching);
}

return false;
}
});
}
}

public boolean isTouching = false;
}
').'
'

NOTE: it is very important NOT to forget to call allowSwipeViewPager(false) if we want to disable swipe
Falling in love with the world

Write : 2015-03-27 15:08:53
Last edit : 2015-03-27 15:25:59

If you want to create a new reply then please login first.



www.OTAK.INFO
Since 19 January 2007
Page hit : 937,067

Code update 24th June 2013
Brain is a very capable to solve big problems
but requires constant reminders about how to.
peace bird