Java android-MapFragment内部滚动视图内部片段
我的目标是让MapFragment处理触摸事件,而不是ScrollView MapFragment以编程方式添加到片段内部的ScrollView中Java android-MapFragment内部滚动视图内部片段,java,android,android-fragments,android-scrollview,supportmapfragment,Java,Android,Android Fragments,Android Scrollview,Supportmapfragment,我的目标是让MapFragment处理触摸事件,而不是ScrollView MapFragment以编程方式添加到片段内部的ScrollView中 <ScrollView android:id="@+id/scrollview_event" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/button_crea
<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();
我在地图上设置了侦听器:
mapFragment.setListener(new FragmentMap.OnTouchListener() {
@Override
public void onTouch() {
scrollView.requestDisallowInterceptTouchEvent(true);
}
});
当在XML布局中声明map fragment时,这应该起作用,但当我以编程方式添加它时,它就不起作用了 要在父视图中启用或禁用拦截事件,您可以使用Android的
onInterceptTouchEvent(MotionEvent ev)
方法。但它是自上而下传播的(即,如果父视图使用事件,则返回true,否则返回false)。因此,创建一个扩展滚动视图的类,并重写该方法:
@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;
}
@覆盖
公共布尔值onInterceptTouchEvent(MotionEvent ev){
如果(检查CoordinateCross(ev,您的目标视图ID)){
返回false;
}
返回true;
}
私有布尔校验坐标交叉(MotionEvent ev,int resId){
查看目标=findViewById(剩余);
if(target==null){
返回false;
}
如果(ev.getX()>target.getX()&&ev.getX()target.getY()&&ev.getY()
请注意,您的\u TARGET \u VIEW \u ID必须是ScrollView的子级,否则findViewById(…)
将返回null
@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;
}