I want to be able to have a Google Maps fragment inside another fragment that is in a ScrollView. I can Scroll down the fragment perfectly, however, when I come to move inside the map It is still in the ScrollView and I have to use two fingers to move around the map.
I have found another answer that shows and provides code that allows you to scroll around the map whilst having a scroll view.
Answer I have been following: how to set google map fragment inside scroll view
The answer has put a .getMap()
which I don't understand where It has came from. Additionally I am unable to get the solution working for my needs.
Here is the code that I have so far:
Inside onCreateView, in PostAJobFragment
if (gMap == null)
{
getChildFragmentManager().findFragmentById(R.id.map);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(new OnMapReadyCallback()
{
@Override
public void onMapReady(GoogleMap googleMap)
{
gMap = googleMap;
}
});
try
{
gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
gMap.getUiSettings().setZoomControlsEnabled(true);
mScrollView = view.findViewById(R.id.advertScrollView); //parent scrollview in xml, give your scrollview id value
((WorkaroundMapFragment) getChildFragmentManager().findFragmentById(R.id.map))
.setListener(new WorkaroundMapFragment.OnTouchListener()
{
@Override
public void onTouch()
{
mScrollView.requestDisallowInterceptTouchEvent(true);
}
});
} catch(NullPointerException e)
{
Log.e(TAG, "Map Creation Exception: " + e.getMessage());
}
}
WorkAroundClass, to get the map to scroll independently of the ScrollView
public class WorkaroundMapFragment extends SupportMapFragment
{
private OnTouchListener mListener;
@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) {
View layout = super.onCreateView(layoutInflater, viewGroup, savedInstance);
TouchableWrapper frameLayout = new TouchableWrapper(getActivity());
frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));
((ViewGroup) layout).addView(frameLayout,
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return layout;
}
public void setListener(OnTouchListener listener) {
mListener = listener;
}
public interface OnTouchListener {
public abstract void onTouch();
}
public class TouchableWrapper extends FrameLayout {
public TouchableWrapper(Context context) {
super(context);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mListener.onTouch();
break;
case MotionEvent.ACTION_UP:
mListener.onTouch();
break;
}
return super.dispatchTouchEvent(event);
}
}
}
PostAJobFragment XML Code
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kitkat.crossroads.Jobs.PostAnAdvertFragment">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/advertScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
// Other Widgets Here
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.kitkat.crossroads.Jobs.PostAnAdvertFragment"
android:name="com.kitkat.crossroads.ExternalClasses.WorkaroundMapFragment" />
My Question Is Different as I am using this in fragments and are getting errors with previous solutions posted