I have a ViewPager inisde of a ScrollView, and the ViewPager dont works correctly. I know, this question is similar, and I googled and read some solutions. Also, behind of the ScrollView I have some stacked fragments. I tried this HorizontalScrollView within ScrollView Touch Handling (extending the viewpager and the scrollview) and it works better, but not perfect. I think that the stacked fragments are capturing the Touch event.
Have somebody this problems? Thanks
EDIT: I add the code.
Custom scrollview:
public class ScrollViewS extends ScrollView {
/* THIS COMMENT CODE IS THE OTHER SOLUTION
private GestureDetector mGestureDetector;
View.OnTouchListener mGestureListener;
public ScrollViewS(Context context, AttributeSet attrs) {
super(context, attrs);
mGestureDetector = new GestureDetector(context, new YScrollDetector());
setFadingEdgeLength(0);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
}
// Return false if we're scrolling in the x direction
class YScrollDetector extends SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if(Math.abs(distanceY) > Math.abs(distanceX)) {
return true;
}
return false;
}
}
*/
private float xDistance, yDistance, lastX, lastY;
public ScrollViewS(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
xDistance = yDistance = 0f;
lastX = ev.getX();
lastY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
final float curX = ev.getX();
final float curY = ev.getY();
xDistance += Math.abs(curX - lastX);
yDistance += Math.abs(curY - lastY);
lastX = curX;
lastY = curY;
if(xDistance > yDistance)
return false;
}
return super.onInterceptTouchEvent(ev);
}
}
ViewPager code:
public class ViewPagerS extends ViewPager {
public ViewPagerS(Context context, AttributeSet attrs) {
super(context, attrs);
}
public boolean onInterceptTouchEvent(MotionEvent ev) {
// Tell our parent to stop intercepting our events!
boolean ret = super.onInterceptTouchEvent(ev);
if (ret) {
getParent().requestDisallowInterceptTouchEvent(true);
}
return ret;
}
}
And the XML is this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/fondoapp"
android:orientation="vertical" >
<com.geo.sov.customviews.ScrollViewS
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/borde_redondeado"
android:id="@+id/scrollview_fragmentaccounts"
>
<LinearLayout
android:id="@+id/x"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:orientation="vertical"
>
<TextView
android:id="@+id/title_accounts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="14dp"
android:textStyle="bold" />
<com.geo.sov.customviews.ViewPagerS
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="95dp"
android:background="#FFFFFFFF" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:padding="10dip"
app:pageColor="#88FF0000"
app:radius="3dp"
app:strokeColor="#FF666666"
app:strokeWidth="1dp" />
(........)
When I start my app, I have one fragment, and if the user logon correctly y change it with the following code:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment f = new AccountsFragment();
fragmentTransaction.add(R.id.fragment, f);
fragmentTransaction.commit();
And I experiment some problems with fragments. When I change to AccountsFragments, if i touch in a empty space, the touch events pass to the logon fragment. I think that the scrollview and viewpager are correctly, and the problem is the fragments that they are intercepting the events.