我正在阅读它,我发现我需要动态创建片段而不是通过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 @R_301_4612@) { MessageData message = (MessageData) messagesList.get(@R_301_4612@); 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(@R_301_4612@,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: No vIEw found for ID 0x1 (unkNown)
for fragment SupportMapFragment{606864b #0 ID=0x1}
非常感谢和亲切的问候,
马塞尔.
错误的原因IllegalArgumentException:找不到ID的视图是在onBindVIEwHolder期间holder.mapContainer和frame还没有附加到RecyclerVIEw / Activity(因此找不到视图错误).
要在调用FragmentTransaction.replace之前保证视图已附加到RecyclerVIEw / Activity,请转而使用onVIEwAttachedToWindow.
class MyAdapter extends RecyclerVIEw.Adapter<MyAdapter.VIEwHolder> { ... @OverrIDe public voID onVIEwAttachedToWindow(VIEwHolder holder) { super.onVIEwAttachedToWindow(holder); // If you have multiple VIEw Type,check for the correct vIEwType SupportMapFragment mapFragment = holder.mapFragment; if (mapFragment == null) { mapFragment = SupportMapFragment.newInstance(); mapFragment.getMapAsync(new OnMapReadyCallback() { @OverrIDe public voID onMapReady(GoogleMap GoogleMap) { ... } }); } FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.ID.map,mapFragment).commit(); }}
https://code.luasoftware.com/tutorials/android/supportmapfragment-in-recyclerview/
总结以上是内存溢出为你收集整理的RecyclerView持有者中的Android Google Maps动态片段全部内容,希望文章能够帮你解决RecyclerView持有者中的Android Google Maps动态片段所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)