我想在ScrollView中有一个MapView,但是当我尝试滚动地图时,ScrollView优先!在地图内滚动时,是否有办法赋予MapView优先级,否则,是否给ScrollView提供优先级?
谢谢!
我想在ScrollView中有一个MapView,但是当我尝试滚动地图时,ScrollView优先!在地图内滚动时,是否有办法赋予MapView优先级,否则,是否给ScrollView提供优先级?
谢谢!
Answers:
我有同样的问题已经有10天了,但是几分钟前我得到了解决方案!这是解决方案。我制作了一个自定义MapView并像这样覆盖onTouchEvent()。
@Override
public boolean onTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
this.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
this.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle MapView's touch events.
super.onTouchEvent(ev);
return true;
}
一种更好/更简单的方法,无需操作单独的触摸事件。如果您使用的是MapView,这将起作用:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
/**
* Request all parents to relinquish the touch events
*/
getParent().requestDisallowInterceptTouchEvent(true);
return super.dispatchTouchEvent(ev);
}
全班:
public class CustomMapView extends MapView {
public CustomMapView(Context context) {
super(context);
}
public CustomMapView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomMapView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomMapView(Context context, GoogleMapOptions options) {
super(context, options);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
/**
* Request all parents to relinquish the touch events
*/
getParent().requestDisallowInterceptTouchEvent(true);
return super.dispatchTouchEvent(ev);
}
}
如果使用的是MapFragment,则可以将片段放入“自定义视图”中,然后dispatchTouchEvent()
进行requestDisallowInterceptTouchEvent
调用。
requestDisallowInterceptTouchEvent
仅必要MotionEvent.ACTION_DOWN
。
制作自己的地图并使用。它完全适合我。
public class CustomMapView extends MapView {
public CustomMapView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_UP:
System.out.println("unlocked");
this.getParent().requestDisallowInterceptTouchEvent(false);
break;
case MotionEvent.ACTION_DOWN:
System.out.println("locked");
this.getParent().requestDisallowInterceptTouchEvent(true);
break;
}
return super.dispatchTouchEvent(ev);
}}
在您的布局xml中,
<com.yourpackage.xxxx.utils.CustomMapView
android:id="@+id/customMap"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
/>
对于那些想要完整的工作代码的人。这里是
自定义地图视图类
public class CustomMapView extends MapView {
private ViewParent mViewParent;
public CustomMapView(Context context) {
super(context);
}
public CustomMapView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomMapView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomMapView(Context context, GoogleMapOptions options) {
super(context, options);
}
public void setViewParent(@Nullable final ViewParent viewParent) { //any ViewGroup
mViewParent = viewParent;
}
@Override
public boolean onInterceptTouchEvent(final MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (null == mViewParent) {
getParent().requestDisallowInterceptTouchEvent(true);
} else {
mViewParent.requestDisallowInterceptTouchEvent(true);
}
break;
case MotionEvent.ACTION_UP:
if (null == mViewParent) {
getParent().requestDisallowInterceptTouchEvent(false);
} else {
mViewParent.requestDisallowInterceptTouchEvent(false);
}
break;
default:
break;
}
return super.onInterceptTouchEvent(event);
}
}
活动布局xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<location.to.your.CustomMapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="250dp"
/>
</ScrollView>
在您的活动或片段中实例化自定义地图类
CustomMapView mapView = (CustomMapView) findViewById(R.id.mapView);
就这样享受
您可以像这样创建自定义MapView:
public class CustomMapView extends MapView {
private MapFragment.ControlLock mCallbackControl;
public CustomMapView(Context context) {
this(context, null);
}
public CustomMapView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomMapView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomMapView(Context context, GoogleMapOptions options) {
super(context, options);
}
public void setCallback(MapFragment.ControlLock callbackControl) {
this.mCallbackControl = callbackControl;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
System.out.println("unlocked");
mCallbackControl.unlock(); /* Interface */
break;
case MotionEvent.ACTION_DOWN:
System.out.println("locked");
mCallbackControl.lock(); /* Interface */
break;
}
return super.dispatchTouchEvent(event);
}
}
我尝试覆盖MapView.onTouchEvent(...),但对我而言不起作用。这是运行良好的代码(覆盖MapView.onInterceptTouchEvent(...)):
public class MyMapView extends MapView {
private ViewParent mViewParent;
//add constructors here
public void setViewParent(@Nullable final ViewParent viewParent) { //any ViewGroup
mViewParent = viewParent;
}
@Override
public boolean onInterceptTouchEvent(final MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (null == mViewParent) {
getParent().requestDisallowInterceptTouchEvent(true);
} else {
mViewParent.requestDisallowInterceptTouchEvent(true);
}
break;
case MotionEvent.ACTION_UP:
if (null == mViewParent) {
getParent().requestDisallowInterceptTouchEvent(false);
} else {
mViewParent.requestDisallowInterceptTouchEvent(false);
}
break;
default:
break;
}
return super.onInterceptTouchEvent(event);
}
}
onInterceptTouchEvent()
也对我有用,而onTouchEvent()
没有。但是我虽然不需要mViewParent,但似乎可以requestDisallowInterceptTouchEvent()
在视图层次结构中传播。
您可以简单地将MapView放在Layout中,并覆盖onTouch或设置Click-Listener-对我来说是最简单的方法,因为我需要在ScrollView中触摸整个MapView。
如果有人想在科特林上这堂课。
我曾经dispatchTouchEvent
像@rotem建议的那样使用
class CustomMapView : MapView {
constructor(context: Context):
super(context)
constructor(context: Context, googleMapOptions: GoogleMapOptions):
super(context, googleMapOptions)
constructor(context: Context, attributeSet: AttributeSet):
super(context, attributeSet)
constructor(context: Context, attributeSet: AttributeSet, int: Int):
super(context, attributeSet, int)
override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
Log.d("CustomWebView", "touchevent")
when(event?.action) {
MotionEvent.ACTION_UP -> {
Log.d("CustomWebView", "disallow Intercept")
parent.requestDisallowInterceptTouchEvent(false)
}
MotionEvent.ACTION_DOWN -> {
Log.d("CustomWebView", "allow Intercept")
parent.requestDisallowInterceptTouchEvent(true)
}
}
return super.dispatchTouchEvent(event)
}
}