首先我知道不推荐使用getMap()方法,但为了避免在map中使用scrollview我使用了这个方法:
创建自定义片段。但是,在那个getMap()点上,我不知道该怎么做。
这是我的自定义片段:
public class WorkaroundMapFragment 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());
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);
}
}
}
这是mapfragment的xml代码:
<fragment xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/guideline3"
tools:context="com.example.mapwithmarker.MapsMarkerActivity"
class="com.example.jonida.restsapp.customFragment.WorkaroundMapFragment"/>
这是我在onViewCreated中的代码,(我在这个项目中没有做任何活动。)
public class OrderDetailsActivityFragment extends Fragment implements OnMapReadyCallback {
@BindView(R.id.scrollView_order_details)
ScrollView mScrollView;
private GoogleMap googleMap;
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
googleMap=((WorkaroundMapFragment)getChildFragmentManager().findFragmentById(R.id.map)).getMap //Here is my problem :/ ;
((WorkaroundMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).setListener(new WorkaroundMapFragment.OnTouchListener() {
@Override
public void onTouch() {
mScrollView.requestDisallowInterceptTouchEvent(true);}});
}
我很感激任何建议,谢谢。
答案 0 :(得分:1)
需要android.gms.maps.GoogleMap找到com.examle.myprojec.WorkAroundFragment
根据您的错误,您需要将 googleMap
声明为 WorkaroundMapFragment
而不是 GoogleMap
使用此
private WorkaroundMapFragment googleMap = (WorkaroundMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
googleMap.getMapAsync(this);
((WorkaroundMapFragment) getChildFragmentManager().findFragmentById(R.id.flysl_map))
.setListener(new WorkaroundMapFragment.OnTouchListener() {
@Override
public void onTouch() {
mScrollView.requestDisallowInterceptTouchEvent(true);
}
});
而不是这个
googleMap=((WorkaroundMapFragment)getChildFragmentManager().findFragmentById(R.id.map)).getMap //Here is my problem :/ ;
((WorkaroundMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).setListener(new WorkaroundMapFragment.OnTouchListener() {
@Override
public void onTouch() {
mScrollView.requestDisallowInterceptTouchEvent(true);}});
修改强>
<fragment xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/guideline3"
tools:context="com.example.mapwithmarker.MapsMarkerActivity"
class="com.example.jonida.restsapp.customFragment.WorkaroundMapFragment"/>