Skip to content

Instantly share code, notes, and snippets.

@Sottti
Last active June 25, 2017 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sottti/890daaeead1bd4784dfce7066a9011aa to your computer and use it in GitHub Desktop.
Save Sottti/890daaeead1bd4784dfce7066a9011aa to your computer and use it in GitHub Desktop.
Allow Android Map to scroll when within an scrollable container
(...)
<fragment
(...)
android:id="@+id/fragment_map"
android:name="com.stackoverflow.aswer.OnScrollableContainerMapFragment"
android:tag="team_details_info_fragment_map" />
(...)
(...)
mSupportMapFragment = (OnScrollableContainerMapFragment) getChildFragmentManager()
.findFragmentById(R.id.fragment_map);
mSupportMapFragment
.setOnTouchListener(new OnScrollableContainerMapFragment.OnTouchListener() {
@Override
public void onStartScrollingMap() {
mScrollView.requestDisallowInterceptTouchEvent(true);
}
@Override
public void onStopScrollingMap() {
mScrollView.requestDisallowInterceptTouchEvent(false);
}
});
(...)
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.google.android.gms.maps.SupportMapFragment;
public class OnScrollableContainerMapFragment extends SupportMapFragment {
private OnTouchListener mOnTouchListener;
@Override
public View onCreateView(
LayoutInflater layoutInflater,
ViewGroup viewGroup,
Bundle savedInstance
) {
final View mapView = super.onCreateView(layoutInflater, viewGroup, savedInstance);
if (mapView != null) {
((ViewGroup) mapView).addView(
new TouchableWrapper(getActivity()),
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
);
}
return mapView;
}
public void setOnTouchListener(@NonNull final OnTouchListener onTouchListener) {
mOnTouchListener = onTouchListener;
}
public interface OnTouchListener {
void onStartScrollingMap();
void onStopScrollingMap();
}
private class TouchableWrapper extends FrameLayout {
public TouchableWrapper(@NonNull final Context context) {
super(context);
setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mOnTouchListener.onStartScrollingMap();
break;
case MotionEvent.ACTION_UP:
mOnTouchListener.onStopScrollingMap();
break;
}
return super.dispatchTouchEvent(event);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment