Android 嵌套ScrollView中的MapView不滚动
像这样在xml中扩展我的MapviewAndroid 嵌套ScrollView中的MapView不滚动,android,google-maps,android-layout,google-play-services,android-maps-v2,Android,Google Maps,Android Layout,Google Play Services,Android Maps V2,像这样在xml中扩展我的Mapview <android.support.v4.widget.NestedScrollView android:id="@+id/sv_offers" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="56dp" android:visib
<android.support.v4.widget.NestedScrollView
android:id="@+id/sv_offers"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="56dp"
android:visibility="gone"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.xys.widgets.CustomMapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="125dp"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
并在活动的onCreate()中初始化了我的mapview
mapView = (CustomMapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
GoogleMap googleMap = mapView.getMap();
googleMap.setMyLocationEnabled(false);
googleMap.getUiSettings().setMyLocationButtonEnabled(false);
googleMap.getUiSettings().setCompassEnabled(false);
googleMap.getUiSettings().setAllGesturesEnabled(false);
mapView.setViewParent(nestedScrollContainer);
其中
nestedScrollContainer
是我的嵌套scrollView。尝试了SO中提供的许多解决方法,但似乎无法找到解决滚动问题的方法。希望您提供帮助!感谢您的代码MapView
中的NestedScrollView-->LinearLayout-->com.xys.widgets.CustomMapView
两级层次结构
因此,在您的情况下,您可以访问NestedScrollView,如下所示
getParent().getParent().requestDisallowInterceptTouchEvent(true);
换行
getParent().RequestDisallowWinterCeptTouchEvent(true)代码>到这个
getParent().getParent().requestDisallowInterceptTouchEvent(true);
下面是您案例的完整代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.xys.widgets.CustomMapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="125dp"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
可以在NestedScrollView中获得几乎方形的MapView
//Fix map height
ViewGroup.LayoutParams lp = mapView.getLayoutParams();
DisplayMetrics metrics = getResources().getDisplayMetrics();
lp.height = metrics.widthPixels;
mapView.setLayoutParams(lp);
您的CustomMapView
的目的是什么?CustomMapView只是MapView的扩展,带有自定义触摸拦截器,用于在ScrollView中启用滚动。我有同样的问题,但我注意到如果我推迟MapView。onCreate(savedInstanceState);例如,在我的片段中,onActivityCreated调用500毫秒后调用,它工作正常。到目前为止,我还没有找到一个解决这个问题的干净方法。请检查这个,如果您有任何疑问,请告诉我。
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 void setViewParent(@Nullable final ViewParent viewParent) { //any ViewGroup
mViewParent = viewParent;
}
public CustomMapView(Context context, GoogleMapOptions options) {
super(context, options);
}
@Override
public boolean onInterceptTouchEvent(final MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
getParent().getParent().requestDisallowInterceptTouchEvent(true);
Timber.d("Inside if of action down");
break;
case MotionEvent.ACTION_UP:
getParent().getParent().requestDisallowInterceptTouchEvent(false);
Timber.d("Inside if of action up");
break;
default:
break;
}
return super.onInterceptTouchEvent(event);
}
}
//Fix map height
ViewGroup.LayoutParams lp = mapView.getLayoutParams();
DisplayMetrics metrics = getResources().getDisplayMetrics();
lp.height = metrics.widthPixels;
mapView.setLayoutParams(lp);