Last active
June 25, 2017 10:14
-
-
Save Sottti/890daaeead1bd4784dfce7066a9011aa to your computer and use it in GitHub Desktop.
Allow Android Map to scroll when within an scrollable container
This file contains 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
(...) | |
<fragment | |
(...) | |
android:id="@+id/fragment_map" | |
android:name="com.stackoverflow.aswer.OnScrollableContainerMapFragment" | |
android:tag="team_details_info_fragment_map" /> | |
(...) |
This file contains 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
(...) | |
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); | |
} | |
}); | |
(...) |
This file contains 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
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