首先,@Rich 感谢您分享您的解决方案 - 这是我一直在寻找的东西。我有一个视图寻呼机,其片段扩展 SupportMapfragment 是其项目之一。在我的解决方案中,我有一个自定义 XML 布局文件,因为我需要向片段添加一些额外的按钮。这是我的解决方案:
public class MapFragmentScrollOverrideViewPager extends ViewPager {
public MapFragmentScrollOverrideViewPager(Context context) {
super(context);
}
public MapFragmentScrollOverrideViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if(v instanceof MapHoldingRelativeLayout) {
return true;
}
return super.canScroll(v, checkV, dx, x, y);
} }
public class MapHoldingRelativeLayout extends RelativeLayout {
public MapHoldingRelativeLayout(Context context) {
super(context);
}
public MapHoldingRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MapHoldingRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} }
片段布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/switchNearbyOffersButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="XXXX"
/>
<xxx.view.MapHoldingRelativeLayout
android:id="@+id/mapLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/switchNearbyOffersButton"
/>
</RelativeLayout>
片段本身:
public final class NearbyOffersFragment extends SupportMapFragment {
public static NearbyOffersFragment newInstance(String content) {
NearbyOffersFragment fragment = new NearbyOffersFragment();
return fragment;
}
private GoogleMap googleMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (googleMap == null) {
googleMap = getMap();
if (googleMap != null) {
setUpMap();
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_nearby_offers,
container, false);
final View mapFragmentLayout = super.onCreateView(inflater, container,
savedInstanceState);
MapHoldingRelativeLayout mapLayout = (MapHoldingRelativeLayout) rootView
.findViewById(R.id.mapLayout);
mapLayout.addView(mapFragmentLayout);
return rootView;
}
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
private void setUpMap() {
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setAllGesturesEnabled(true);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(41.474856, -88.056928))
.zoom(14.5f)
.build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
由于原始 SupportMapFragment 的布局在我的自定义 RelativeLyout 内,因此您可以使用 instanceof 运算符而不是直接引用 maps.jb 类...