谷歌地图片段在 NestedScrollView 内滚动
Posted
技术标签:
【中文标题】谷歌地图片段在 NestedScrollView 内滚动【英文标题】:Google Map Fragment scrolling inside NestedScrollView 【发布时间】:2017-06-01 02:14:05 【问题描述】:我有一个NestedScrollView
作为父级和包含googleMap
作为其子级的片段。当我在地图中向下或向上滚动时,NesetdScrollView 是滚动的,我无法在地图内滚动。到目前为止,我已经尝试通过创建透明图像来解决堆栈溢出问题,但它对我不起作用。
XML:
<android.support.v4.widget.NestedScrollView
android:layout_
android:layout_
android:layout_below="@+id/car_progress"
android:id="@+id/nested_scroll"
android:visibility="gone"
>
............
............
<RelativeLayout
android:layout_
android:layout_
android:layout_below="@id/first_section"
android:visibility="gone"
android:id="@+id/map_wrapper"
android:orientation="vertical"
android:layout_marginTop="10dp"
>
<TextView
android:layout_
android:layout_
android:text="@string/dealer_location"
android:textAllCaps="true"
android:textColor="@color/colorPrimary"
style="@style/BoldStyle"
android:layout_marginLeft="@dimen/single_title_dimen"
android:layout_marginRight="@dimen/single_title_dimen"
android:id="@+id/dealer_head"
/>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="@+id/dealer_head"
/>
<ImageView
android:layout_
android:layout_
android:id="@+id/imagetrans"
android:layout_alignTop="@+id/map"
android:layout_alignBottom="@+id/map"
android:layout_alignEnd="@+id/map"
android:layout_alignRight="@+id/map"
android:layout_alignLeft="@+id/map"
android:layout_alignStart="@+id/map"
android:src="@android:color/transparent"/>
</RelativeLayout>
.....
.....
</android.support.v4.widget.NestedScrollView>
代码:
transImg.setOnTouchListener(new View.OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
int action = event.getAction();
switch (action)
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
nv.requestDisallowInterceptTouchEvent(true);
// Disable touch on transparent view
return false;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
nv.requestDisallowInterceptTouchEvent(false);
return true;
case MotionEvent.ACTION_MOVE:
nv.requestDisallowInterceptTouchEvent(true);
return false;
default:
return true;
);
【问题讨论】:
【参考方案1】:试试这个...使用这个带有可触摸包装器的自定义地图片段。
MySupportMapFragment mSupportMapFragment;
mSupportMapFragment = (MySupportMapFragment) getChildFragmentManager().findFragmentById(R.id.googleMap);
if(mSupportMapFragment != null)
mSupportMapFragment.setListener(new MySupportMapFragment.OnTouchListener()
@Override
public void onTouch()
scrollView.requestDisallowInterceptTouchEvent(true);
);
MySupportMapFragment
public class MySupportMapFragment extends SupportMapFragment
private OnTouchListener mListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
View layout = super.onCreateView(inflater, parent, savedInstanceState);
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);
【讨论】:
我无法从活动中访问 getChildFragmentManager()。 java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment 无法转换为 com.bidhee.bottlesup.mvp.view.customview.MySupportMapFragment 完美!很好的解决方案。工作正常。 我的问题通过从我的 Activity 内部片段映射标记的 XML 文件中更改这行代码android:name="com.google.android.gms.maps.SupportMapFragment"
和这一行 android:name="com.development.passengerapp.map.MySupportMapFragment"
解决了,谢谢@AshishJohn
最佳解决方案:)以上是关于谷歌地图片段在 NestedScrollView 内滚动的主要内容,如果未能解决你的问题,请参考以下文章