2016-11-14 59 views
1

我已经添加了地图片段到ListView。它工作正常,但无法拖动或缩放。不能拖动或缩放列表视图内的谷歌地图android

MainActivity.java

mapviewheader = getActivity().getLayoutInflater().inflate(R.layout.view_header, mListView, false); 
FrameLayout map_container = (FrameLayout) mapviewheader.findViewById(R.id.map_container); 


FragmentTransaction mTransaction = this.getChildFragmentManager().beginTransaction(); 
mapFragment = new SupportMapFragment().newInstance(); 

//mapFragment.setOnTouchListener 
mTransaction.add(map_container.getId(), mapFragment); 
mTransaction.commit(); 

try { 
    MapsInitializer.initialize(getActivity()); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

mListView.setParallaxView(mapviewheader); 

view_header.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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:orientation="vertical"> 

    <FrameLayout 
     android:id="@+id/map_container" 
     android:layout_width="match_parent" 
     android:layout_height="400dp" 
     /> 

</LinearLayout> 

我曾尝试使用onTouchListener(...),但它并没有为我工作。

回答

1

这是因为ListView已经在捕捉触摸事件。你必须让你的地图片段抓住他们。为此,您可以扩展您正在使用的MapSupportFragment

public class TouchableGoogleMapFragment extends SupportMapFragment { 
    private OnTouchListener mListener; 

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

     TouchableWrapper frameLayout = new TouchableWrapper(getActivity()); 

     // make it invisible 
     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); 
     } 
    } 
} 

,然后在你的MainActivity你必须requestDisallowInterceptTouchEvent(true)当用户触摸片段:

// Intercepting touch events 
mapFragment.setListener(new TouchableGoogleMapFragment.OnTouchListener() { 
    @Override 
    public void onTouch() { 
     mScrollView.requestDisallowInterceptTouchEvent(true); 
    } 
}); 
+0

我已经使用这个片段里面和错误显示了当我写mapFragment.setListener –

+0

它给你什么错误? –

+0

红色错误没有那种方法 –