1

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

6
  • it is a bad UI/UX to have scrollable map area insde a scrollview. Commented Apr 6, 2018 at 21:12
  • The user on the PostAJobFragment can enter the jobs name, description etc and then select a location from a map. If I put the map on a seperate fragment, I will have to pass all the JobInformation into the MapFragment and back again to repopulate the PostAJobFragment, which is too much memory and inefficent and could produce errors. I therefore, must put it on the same page. Commented Apr 6, 2018 at 21:16
  • I have relooked over the answer provided (The other question) and typed in the code given. When I type in getSupportFragmentManager().findFragmentById(R.id.map)).getMap(). getSupportFragmentManager is red as I'm working in a fragment. I tried using getFragmentManger but that makes .getMap() unavaliable Commented Apr 6, 2018 at 21:18
  • what if you keep your map pinned on the top and let the scrollview be below it. like this maybe? cdn.techpp.com/wp-content/uploads/2015/07/… Commented Apr 6, 2018 at 21:20
  • Try maybe SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); map = mapFrag.getMap(); Commented Apr 6, 2018 at 21:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.