当我从API获取位置信息时,我必须膨胀一个新的布局并添加它 但是MapView和FrameLayout有时无法点击和触摸。
我已经在我的NestScrollView类中实现了此代码以处理事件
我的NestScrollView类
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (checkCoordinateCross(ev, R.id.btn_to_map)) {
return false;
}
if (checkCoordinateCross(ev, R.id.map_view)) {
return false;
}
if (checkCoordinateCross(ev, R.layout.summary_content_location_test2_component)) {
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;
}
我的活动
View summaryShowLayout = findViewById(R.id.root_linear_layout);
LinearLayout summaryContentsLayout = (LinearLayout) summaryShowLayout.findViewById(R.id.summaryContents);
try
{
final LinearLayout summaryContentLocationTest = (LinearLayout) getLayoutInflater().inflate(R.layout.summary_content_location_test2, null);
final LatLng latLng = new LatLng(Double.parseDouble(summaryJson.getString("lat")), Double.parseDouble(summaryJson.getString("long")));
Double myLatitude = latLng.latitude;
Double myLongitude = latLng.longitude;
String labelLocation = finalTitleLocation;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:<" + myLatitude + ">,<" + myLongitude + ">?q=<" + myLatitude + ">,<" + myLongitude + ">(" + labelLocation + ")"));
Uri uri = Uri.parse("geo:<" + myLatitude + ">,<" + myLongitude + ">?q=<" + myLatitude + ">,<" + myLongitude + ">(" + labelLocation + ")");
String link = uri.toString();
MapView mv = (MapView) summaryContentLocationTest.findViewById(R.id.map_view);
mv.setClickable(true);
mv.setFocusable(true);
mv.setDuplicateParentStateEnabled(false);
mv.onCreate(new Bundle());
mv.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setMapToolbarEnabled(false);
googleMap.setPadding(0, 500, 0, 0);
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
googleMap.setMyLocationEnabled(true);
MapsInitializer.initialize(SummaryActivity.this);
googleMap.addMarker(new MarkerOptions().position(latLng).title(finalTitleLocation));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));
}
});
mv.onResume();
final View frameView = getLayoutInflater().inflate(R.layout.summary_content_location_test2_component, null);
View btnToMap = frameView.findViewById(R.id.btn_to_map);
btnToMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(SummaryActivity.this, "asdfasdf", Toast.LENGTH_SHORT).show();
Double myLatitude = latLng.latitude;
Double myLongitude = latLng.longitude;
String labelLocation = finalTitleLocation;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:<" + myLatitude + ">,<" + myLongitude + ">?q=<" + myLatitude + ">,<" + myLongitude + ">(" + labelLocation + ")"));
startActivity(intent);
}
});
summaryContentsLayout.addView(summaryContentLocationTest);
summaryContentsLayout.addView(frameView);
} catch(
JSONException e)
{
e.printStackTrace();
}
我的summary_content_location_test2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linear_layout_for_mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.gms.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="300dp">
</com.google.android.gms.maps.MapView>
我的summary_content_location_test2._component.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/btn_to_map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/colorAccent"
android:gravity="center"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="Open Google Map"
android:textColor="@color/colorWhite"
android:textSize="20sp" />
</FrameLayout>