Skip to content

Instantly share code, notes, and snippets.

@kibotu
Created December 26, 2016 17:09
Show Gist options
  • Save kibotu/326b8ae31b0ba111aacab3d3657fca51 to your computer and use it in GitHub Desktop.
Save kibotu/326b8ae31b0ba111aacab3d3657fca51 to your computer and use it in GitHub Desktop.
Android: Fixing vertical/horizontal scrolling for nested maps.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/actionbar"
android:id="@+id/scrollView"
android:descendantFocusability="afterDescendants">
<RelativeLayout
android:id="@+id/mapsContainer"
android:layout_width="match_parent"
android:layout_height="127dp"
android:layout_below="@+id/gravatar"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="20dp">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:layout="@layout/map_preview" />
<ImageView
android:id="@+id/transparentImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@android:color/transparent" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
/**
* @see [nested scrolling workaround](http://stackoverflow.com/a/17317176)
*/
private fun fixMapScrolling() {
transparentImage.setOnTouchListener { v, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
// Disallow ScrollView to intercept touch events.
scrollView.requestDisallowInterceptTouchEvent(true)
// Disable touch on transparent view
false
}
MotionEvent.ACTION_UP -> {
// Allow ScrollView to intercept touch events.
scrollView.requestDisallowInterceptTouchEvent(false)
true
}
MotionEvent.ACTION_MOVE -> {
scrollView.requestDisallowInterceptTouchEvent(true)
false
}
else -> {
true
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment