2013-04-07 82 views
3

我有一个活动,有一个滚动视图,计数很多东西......其中之一是mapfragment api v2。大和行车问题是当我垂直移动地图时,所有的滚动移动,我不想要这个。当我触摸并移动地图时,我确实希望滚动视图不要移动。Android:滚动视图拦截地图API V2触摸

的代码是这样的:

public class MyMap extends FragmentActivity { 

... 



     @Override 
     public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    setContentView(R.layout.map); 

      ... 


    fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfoto); 
    googleMap = fm.getMap(); 


    /** Here i can do something like googleMap.setOnToucListener().... 
      but it's not supported!!!! */ 
      ... 

    } 

} 

和map.xml:

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/scroll" 
    tools:context=".MyMap"> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 

     <RelativeLayout 
      android:id="@+id/relativeLayoutA" 
      android:layout_below="@+id/mapfoto" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 
      <fragment 
       android:id="@+id/map" 
       android:layout_width="match_parent" 
       android:layout_height="400dp" 
       class="com.google.android.gms.maps.SupportMapFragment" /> 

     ... 

      </RelativeLayout> 
     ... 

     </RelativeLayout> 


</ScrollView>  

我试着使用OnMapClickListener()和OnMapLongClickListener(),但没有奏效

所有的帮助都是很好的...

回答

0

这个想法是在地图片段wh上拦截触摸事件滚动动作发生在地图片段区域。

可以扩展MapFragment并把它顶部的FrameLayout,其截取和通信的触摸动作。

import android.content.Context; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.v4.content.ContextCompat; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.FrameLayout; 
import com.google.android.gms.maps.SupportMapFragment; 

public class OnScrollableContainerMapFragment extends SupportMapFragment { 

    private OnTouchListener mOnTouchListener; 

    @Override 
    public View onCreateView(
     LayoutInflater layoutInflater, 
     ViewGroup viewGroup, 
     Bundle savedInstance 
) { 
    final View mapView = super.onCreateView(layoutInflater, viewGroup, savedInstance); 

    if (mapView != null) { 
     ((ViewGroup) mapView).addView(
      new TouchableWrapper(getActivity()), 
      new ViewGroup.LayoutParams(
       ViewGroup.LayoutParams.MATCH_PARENT, 
       ViewGroup.LayoutParams.MATCH_PARENT 
     ) 
    ); 
    } 

    return mapView; 
    } 

    public void setOnTouchListener(@NonNull final OnTouchListener onTouchListener) { 
    mOnTouchListener = onTouchListener; 
    } 

    public interface OnTouchListener { 

    void onStartScrollingMap(); 

    void onStopScrollingMap(); 
    } 

    private class TouchableWrapper extends FrameLayout { 

    public TouchableWrapper(@NonNull final Context context) { 
     super(context); 
     setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent)); 
    } 

    @Override 
    public boolean dispatchTouchEvent(MotionEvent event) { 
     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      mOnTouchListener.onStartScrollingMap(); 
      break; 
     case MotionEvent.ACTION_UP: 
      mOnTouchListener.onStopScrollingMap(); 
      break; 
     } 
     return super.dispatchTouchEvent(event); 
    } 
    } 

} 

当滚动地图上的动作开始,你问外滚动容器停止拦截触摸事件。

当地图上的滚动完成时,您告诉外部可滚动容器再次开始拦截触摸事件。

在下面的例子中,外部可滚动容器是一个ScrollView。

请注意,该示例假定代码位于片段内。只是整个类文件的一部分,并没有其他必要的元素来编译。

(...) 
mSupportMapFragment = (OnScrollableContainerMapFragment) getChildFragmentManager() 
     .findFragmentById(R.id.fragment_map); 

     mSupportMapFragment 
     .setOnTouchListener(new OnScrollableContainerMapFragment.OnTouchListener() { 
      @Override 
      public void onStartScrollingMap() { 
      mScrollView.requestDisallowInterceptTouchEvent(true); 
      } 

      @Override 
      public void onStopScrollingMap() { 
      mScrollView.requestDisallowInterceptTouchEvent(false); 
      } 
     }); 
(...) 

XML布局文件如下所示。

请注意,这是文件的只是一部分,并且缺乏其他必要的元素,使其编译。

(...) 

<fragment 
      (...) 
      android:id="@+id/fragment_map" 
      android:name="com.stackoverflow.answer.OnScrollableContainerMapFragment" 
      android:tag="team_details_info_fragment_map" /> 
(...) 

看一看要点在这里:Gist