2016-11-14 41 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

紅色錯誤沒有那種方法 –