3

充氣我的MapView在這樣的XMLMapView的內部NestedScrollView不滾動

<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如下: -

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); 
    } 
} 

而且intilized我在的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.Tried許多worka但似乎沒有得到滾動問題的解決方法。幫助將不勝感激!謝謝

+0

你的'CustomMapView'的目的是什麼? – Androiderson

+0

CustomMapView不過是帶有自定義觸摸攔截器的MapView的擴展,可以在滾動視圖中滾動 – goonerDroid

+3

我有同樣的問題,但我注意到如果我推遲了mapView.onCreate(savedInstanceState);例如,在我的片段中的onActivityCreated調用之後調用500ms後,它正常工作。到目前爲止,我沒有找到這個問題的乾淨解決方案。 – Bibu

回答

2

在您的代碼MapView裏面NestedScrollView--> LinearLayout--> com.xys.widgets.CustomMapView兩層的層次結構。

所以你的情況,你可以訪問NestedScrollView像下面

getParent().getParent().requestDisallowInterceptTouchEvent(true); 

改變這一行 getParent().requestDisallowInterceptTouchEvent(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> 

`

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); 
     } 
    } 
0

有可能進去NestedScrollView幾乎是正方形的MapView。

//Fix map height 
    ViewGroup.LayoutParams lp = mapView.getLayoutParams(); 
    DisplayMetrics metrics = getResources().getDisplayMetrics(); 
    lp.height = metrics.widthPixels; 
    mapView.setLayoutParams(lp); 
相關問題