Reputation: 31
I have an Activity that have a scrollview that countains a lot of things... one of these is a mapfragment api v2. The BIG AND TROLLING PROBLEM is when I move the map vertically, ALL the Scroll moves, and i don't want this. I really want that scrollview DONT MOVE when I touch and move the map.
The code is like:
public class MyMap extends FragmentActivity {
...
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.map);
...
fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfoto);
googleMap = fm.getMap();
/** Here i can do something like googleMap.setOnToucListener()....
but it's not supported!!!! */
...
}
}
and the map.xml:
<ScrollView
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:layout_below="@+id/scroll"
tools:context=".MyMap">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/relativeLayoutA"
android:layout_below="@+id/mapfoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="400dp"
class="com.google.android.gms.maps.SupportMapFragment" />
...
</RelativeLayout>
...
</RelativeLayout>
</ScrollView>
I've tried to use OnMapClickListener() and OnMapLongClickListener() but didn't work
All the help is wellcome...
Upvotes: 3
Views: 548
Reputation: 14389
The idea is to intercept touch events just on the map fragment while the scrolling action is happening on the map fragment area.
You can extend the MapFragment and put on top of it a FrameLayout that intercepts and communicates the touch actions.
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);
}
}
}
When the action of scrolling on the map starts, you ask the outer scrollable container to stop intercepting touch events.
When the scrolling on the map finishes then you tell the outer scrollable container to start intercepting touch events again.
In the example below the outer scrollable container is a ScrollView.
Please note the example assumes the code is within a Fragment. Is just a portion of the whole class file and lacking necessary elements in other to make it compile.
(...)
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);
}
});
(...)
The XML layout file would look like below.
Please note that this is just a portion of the file and is lacking necessary elements in other to make it compile.
(...)
<fragment
(...)
android:id="@+id/fragment_map"
android:name="com.stackoverflow.answer.OnScrollableContainerMapFragment"
android:tag="team_details_info_fragment_map" />
(...)
Take a look at the gist here: Gist
Upvotes: 1