我的目标是让MapFragment处理触摸事件而不是ScrollView。
MapFragment是在ScrollView内部以编程方式添加的,它位于Fragment内部。
<ScrollView
android:id="@+id/scrollview_event"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/button_create"
android:layout_alignParentTop="true" >
<LinearLayout
android:id="@+id/linearlayout_event"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_light"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearlayout_where"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/linearlayout_header_event"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textview_where"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="@string/where"
android:textColor="@color/text_dark"
android:textSize="@dimen/text_heading" />
<View
android:id="@+id/view_divider_where"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_below="@id/textview_where"
android:layout_marginLeft="10dp"
android:layout_marginRight="30dp"
android:background="@color/bg_dark" />
<ImageView
android:id="@+id/imageview_close_event"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:clickable="true"
android:onClick="onClickCloseEvent"
android:src="@drawable/close_dark" />
</RelativeLayout>
<FrameLayout
android:id="@+id/framelayout_map"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginTop="10dp" >
<FrameLayout
android:id="@+id/fragment_map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/imageview_center_event"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="top"
android:layout_margin="6dp"
android:background="@color/bg_transparent_light"
android:clickable="true"
android:padding="6dp"
android:src="@drawable/event_pin" />
<ImageView
android:id="@+id/imageview_my_position"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="bottom|left"
android:layout_margin="6dp"
android:background="@color/bg_transparent_light"
android:clickable="true"
android:padding="6dp"
android:src="@drawable/gps_target" />
</FrameLayout>
<TextView
android:id="@+id/textview_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/bg_dark"
android:padding="6dp"
android:textColor="@color/text_light"
android:textSize="@dimen/text_address"
android:visibility="visible" />
</LinearLayout>
//other views and layouts...
</LinearLayout>
</ScrollView>
mapFragment = new FragmentMap();
getChildFragmentManager().beginTransaction().add(R.id.fragment_map, mapFragment, TAG).commit();
getChildFragmentManager().executePendingTransactions();
FragmentMap看起来像这样:
public class FragmentMap extends SupportMapFragment {
private OnTouchListener mListener;
private boolean mTouchListener = false;
@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstance) {
View layout = super.onCreateView(layoutInflater, viewGroup, savedInstance);
TouchableWrapper frameLayout = new TouchableWrapper(getActivity());
frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));
frameLayout.addView(layout);
setListener(new OnTouchListener() {
@Override
public void onTouch() {
//Prevents NullPointerException
}
});
return frameLayout;
}
public void setListener(OnTouchListener listener) {
mListener = listener;
}
public interface OnTouchListener {
public abstract void onTouch();
}
public boolean getTouch() {
return mTouchListener;
}
public class TouchableWrapper extends FrameLayout {
public TouchableWrapper(Context context) {
super(context);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mTouchListener = true;
break;
case MotionEvent.ACTION_UP:
mTouchListener = false;
break;
}
mListener.onTouch();
return super.dispatchTouchEvent(event);
}
}
}
我在地图上设置了监听器:
mapFragment.setListener(new FragmentMap.OnTouchListener() {
@Override
public void onTouch() {
scrollView.requestDisallowInterceptTouchEvent(true);
}
});
这应该在XML布局中声明地图片段时起作用,但是当我以编程方式添加时,它不会。
答案 0 :(得分:3)
要在父视图中启用或禁用拦截事件,您需要使用Android的onInterceptTouchEvent(MotionEvent ev)
方法。但它是从自上而下传播的(即父视图如果消耗了事件则返回true,否则返回false)。因此,创建一个扩展ScrollView
的类,并覆盖该方法:
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(checkCoordinateCross(ev, YOUR_TARGET_VIEW_ID)) {
return false;
}
return true;
}
private boolean checkCoordinateCross(MotionEvent ev, int resId) {
View target = findViewById(resId);
if(target == null) {
return false;
}
if(ev.getX() > target.getX() && ev.getX() < target.getX() + target.getWidth() && ev.getY() > target.getY() && ev.getY() < target.getY() + target.getHeight()) {
return true;
}
return false;
}
请注意,YOUR_TARGET_VIEW_ID必须是ScrollView的子项,否则findViewById(...)
将返回null。