NestedScrollView中的MapView无法滚动 [英] MapView inside NestedScrollView not scrolling
问题描述
像这样将我的Mapview扩展为xml
Inflating my Mapview in xml like this
<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>
我已经实现了自定义Mapview,如下所示:-
And i have implemented the Custom Mapview as follows:-
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:
if (null == mViewParent) {
getParent().requestDisallowInterceptTouchEvent(true);
Timber.d("Inside if of action down");
} else {
mViewParent.requestDisallowInterceptTouchEvent(true);
Timber.d("Inside else of action down");
}
break;
case MotionEvent.ACTION_UP:
if (null == mViewParent) {
getParent().requestDisallowInterceptTouchEvent(false);
Timber.d("Inside if of action up");
} else {
mViewParent.requestDisallowInterceptTouchEvent(false);
Timber.d("Inside else of action up");
}
break;
default:
break;
}
return super.onInterceptTouchEvent(event);
}
}
并在我的Activity的onCreate()中引入了我的mapview
And intilized my mapview in onCreate() of my Activity
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中提供的许多解决方法,但似乎无法获得解决滚动问题的方法.不胜感激!谢谢
Where nestedScrollContainer
is my nested scrollView.Tried many workarounds provided in SO but cant seem to get a workaround for scrolling issue.Help would be appreciated!Thanks
推荐答案
在您的代码MapView
内部NestedScrollView--> LinearLayout--> com.xys.widgets.CustomMapView
两级层次结构中.
In your code MapView
inside NestedScrollView--> LinearLayout--> com.xys.widgets.CustomMapView
Two level hierarchy.
因此,您可以按照以下方式访问NestedScrollView
So in your case you can access NestedScrollView like below
getParent().getParent().requestDisallowInterceptTouchEvent(true);
更改此行
getParent().requestDisallowInterceptTouchEvent(true);
对此
getParent().getParent().requestDisallowInterceptTouchEvent(true);
以下是您案子的完整代码
Below is full code for your case
<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>
`
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);
}
}
这篇关于NestedScrollView中的MapView无法滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!