對於我來說,添加一個透明的ImageView並不能幫助完全去除黑色遮罩。地圖的頂部和底部仍然顯示滾動時的黑色遮罩。
所以對於它的解決方案,我發現在this answer有一個小的變化。 我說,
android:layout_marginTop="-100dp"
android:layout_marginBottom="-100dp"
我的地圖碎片,因爲它是垂直滾動視圖。所以,我的佈局現在看起來是這樣的:
<RelativeLayout
android:id="@+id/map_layout"
android:layout_width="match_parent"
android:layout_height="300dp">
<fragment
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="-100dp"
android:layout_marginBottom="-100dp"
android:name="com.google.android.gms.maps.MapFragment"/>
<ImageView
android:id="@+id/transparent_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@color/transparent" />
</RelativeLayout>
爲了解決我爲我的主滾動型設置requestDisallowInterceptTouchEvent(true)
問題的第二部分。當用戶觸摸的透明圖像和感動我禁用了透明圖像上的觸摸爲MotionEvent.ACTION_DOWN
和MotionEvent.ACTION_MOVE
,這樣的地圖碎片可以採取觸摸事件。
ScrollView mainScrollView = (ScrollView) findViewById(R.id.main_scrollview);
ImageView transparentImageView = (ImageView) findViewById(R.id.transparent_image);
transparentImageView.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.
mainScrollView.requestDisallowInterceptTouchEvent(true);
// Disable touch on transparent view
return false;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
mainScrollView.requestDisallowInterceptTouchEvent(false);
return true;
case MotionEvent.ACTION_MOVE:
mainScrollView.requestDisallowInterceptTouchEvent(true);
return false;
default:
return true;
}
}
});
這對我有效。希望它可以幫助你..
「我有一個滾動型新MapFragments之一」 - 我會感到驚訝,如果這個效果很好。 「我怎樣才能讓MapFragment使用我自定義的MapView而不是標準的?」 - 你不能,AFAIK。你將不得不創建自己的片段來託管你的'MapView'。 – CommonsWare
我也面臨同樣的問題,但我在片段中使用原始MapView。同樣的問題,當在ScrollView或ViewPager中滾動時,黑色面具被留下。 –
在我的Nexus S(Android 4.1.2)上,我沒有收到這個問題。不過,在我測試過的所有Android 2.2-2.3設備上,我遇到了完全相同的問題。 – Ralf