【发布时间】:2017-06-12 16:21:42
【问题描述】:
我正在尝试将 Google 地图片段添加到 Android 中的 RecyclerView 中。
我正在阅读它,我发现我需要动态创建片段而不是通过 XML 来避免重复 ID 错误。
这是我的适配器代码:
public class MapViewHolder extends RecyclerView.ViewHolder {
protected FrameLayout mapContainer;
public MapViewHolder(View v){
super(v);
view = v;
mapContainer = (FrameLayout) v.findViewById(R.id.mapContainer);
}
}
private void bindMapRow(final MapViewHolder holder, int position) {
MessageData message = (MessageData) messagesList.get(position);
LocationData location = message.getLocation();
FrameLayout view = holder.mapContainer;
FrameLayout frame = new FrameLayout(context);
if (message.getIdMap() != 0) {
frame.setId(message.getIdMap());
}
else {
message.setIdMap(generateViewId());
frame.setId(message.getIdMap());
messagesList.set(position, message);
}
int size = context.getResources().getDimensionPixelSize(R.dimen.row_chat_map_size);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(size, size);
frame.setLayoutParams(layoutParams);
view.addView(frame);
GoogleMapOptions options = new GoogleMapOptions();
options.liteMode(true);
SupportMapFragment mapFrag = SupportMapFragment.newInstance(options);
mapFrag.getMapAsync(new MapReadyCallback(location));
FragmentManager fm = activity.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(frame.getId(), mapFrag);
ft.commit();
}
执行此操作,我可以看到地图显示在我的 RecyclerView 中,并且一切似乎都运行良好。 当我在 RecyclerView 中向上滚动一段时间然后再次返回地图时,就会出现问题。
当我这样做时,应用程序崩溃并显示此错误:
java.lang.IllegalArgumentException:未找到 id 0x1 的视图(未知) 对于片段 SupportMapFragment{606864b #0 id=0x1}
非常感谢和亲切的问候, 马塞尔。
【问题讨论】:
-
没人能帮我吗?
标签: android google-maps android-recyclerview fragment google-maps-android-api-2