gpt4 book ai didi

Android 自定义 View 和嵌套 fragment

转载 作者:行者123 更新时间:2023-11-29 01:17:01 26 4
gpt4 key购买 nike

我的 View 中有一个父 fragment ,其中以编程方式添加了 2 个子 fragment 以显示 map 。这是子 fragment 的布局, map 在 FrameLayout 中呈现。

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中有一个 map 加载的点击事件。我的问题是,即使 FrameLayout View 已成功膨胀(因为我可以看到两个子 fragment 的调试文本“ map 在此处加载”),但第二个子 fragment 的 map 在第一个子 fragment 本身中呈现(我看到它正在刷新当触发第二个 map 点击监听器时)。这里可能出了什么问题?

在布局之上膨胀的类,然后将其添加到父 fragment :

位置字段.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));
}
}

最佳答案

通过使用方法 generateViewId() 为 map 动态设置 FrameLayouts 的 id 解决了这个问题。

看起来 FragmentManager 正在覆盖之前添加的帧的帧 ID。 (因为他们都有相同的ID??)

共享解决问题所需的代码更改(以防其他人遇到此问题):

  1. 为框架布局动态生成 ID。一开始会有您 View 中框架布局的一个 ID。获取 View 对象并分配新生成的 View ID。

    mMapFrameId = View.generateViewId();

  2. 在 fragment 交易中使用相同的 ID,同时添加新的 fragment 。

    mMapFragment = new MapFragment();
    FragmentTransaction transaction = mFragmentManager.beginTransaction();
    transaction.add(mMapFrameId, mMapFragment);
    transaction.addToBackStack(null);
    transaction.commit();

关于Android 自定义 View 和嵌套 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38902745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com