我在我的視圖中有一個父片段,其中2個子片段以編程方式添加到顯示地圖。這是子片段的佈局,地圖在FrameLayout中呈現。Android自定義視圖和嵌套片段
location_field_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/settings_action_item_row_style"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="256dp"
android:id="@+id/map_fragment_container"
android:layout_marginTop="2dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is where map loads."/>
</FrameLayout>
</LinearLayout>
沒有爲相應FrameLayouts要加載地圖點擊事件。我的問題是,即使FrameLayout視圖成功膨脹(因爲我可以看到兩個子片段的調試文本「地圖加載在這裏」),但第二個子片段的地圖在第一個子片段本身呈現(我看到它刷新當第二個地圖點擊偵聽器被觸發時)。這裏可能會出現什麼問題?
類充氣上述佈局,然後將其加入到母體片段:
LocationField.java
public class LocationField extends LinearLayout
{
private MyMapFragment mMapFragment;
public LocationField()
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.location_field_view, this, true);
addMapFragment();
//mMapFragment.loadMap() method gets triggered based on a click event.
}
private void addMapFragment()
{
mMapFragment = MyMapFragment.newInstance(lat, long, marker);
FragmentTransaction transaction = mParentFragment.getChildFragmentManager().beginTransaction();
transaction.add(R.id.map_fragment_container, mMapFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
MyMapFragment.java
public class MyMapFragment extends MapFragment implements OnMapReadyCallback
{
private static final String KEY_MARKER = "marker";
private static final String KEY_LATITUDE = "latitude";
private static final String KEY_LONGITUDE = "longitude";
private LatLng mLatLng;
private String mMarker;
public static MyMapFragment newInstance(Double latitude, Double longitude, String marker){
MyMapFragment myMapFragment = new MyMapFragment();
Bundle arguments = new Bundle();
arguments.putDouble(KEY_LATITUDE, latitude);
arguments.putDouble(KEY_LONGITUDE, longitude);
arguments.putString(KEY_MARKER, marker);
myMapFragment.setArguments(arguments);
return myMapFragment;
}
public void onCreate(Bundle savedInstance)
{
super.onCreate(savedInstance);
Bundle arguments = getArguments();
mLatLng = new LatLng(arguments.getDouble(KEY_LATITUDE), arguments.getDouble(KEY_LONGITUDE));
mMarker = arguments.getString(KEY_MARKER);
}
public void loadMap()
{
getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
if (googleMap == null) {
return;
}
googleMap.addMarker(new MarkerOptions().position(mLatLng).title(mMarker));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLatLng, 10f));
}
}