Android谷歌地图动态片段RecyclerView持有人 [英] Android Google Maps dynamic fragment in RecyclerView holder
问题描述
我试图将Google地图片段添加到Android中的RecyclerView中。
我正在阅读它,并且看到我需要动态创建片段,而不是通过XML来实现,以避免重复的ID错误。
$ b
以下是我的适配器代码:
public class MapViewHolder extends RecyclerView.ViewHolder {
受保护的FrameLayout mapContainer;
public MapViewHolder(View v){
super(v);
view = v;
mapContainer =(FrameLayout)v.findViewById(R.id.mapContainer);
private void bindMapRow(final MapViewHolder持有者,int位置){
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();
$ / code>
执行此操作,我可以看到显示在我的RecyclerView中的地图,运作良好。
当我在RecyclerView中向上滚动一段时间然后再返回到地图时,会出现问题。
当我这样做时,应用程序崩溃并显示此错误:
java .lang.IllegalArgumentException:没有找到id为0x1的视图(未知)
为片段SupportMapFragment {606864b#0 id = 0x1}
非常感谢和关心,
Marcel。
onBindViewHolder
。 错误原因 IllegalArgumentException :没有找到id
的视图是 holder.mapContainer
和 frame
未附加到RecyclerView / Activity在 onBindViewHolder
期间(因此视图未找到错误)。
为了保证视图已经存在在调用 FragmentTransaction.replace
之前,先附加到RecyclerView / Activity上,然后监听 onViewAttachedToWindow
。
class MyAdapter扩展了RecyclerView.Adap之三< MyAdapter.ViewHolder> {
...
@Override
public void onViewAttachedToWindow(ViewHolder持有者){
super.onViewAttachedToWindow(持有者);
//如果您有多个视图类型,请检查正确的视图类型
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/
I'm trying to add Google Maps fragments into a RecyclerView in Android.
I was reading about it and I saw that I need to create the fragments dynamically instead of doing it by XML to avoid the duplicate ID errors.
Here is my adapter code:
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();
}
Executing this I can see the map displayed into my RecyclerView and all seems to be working well. The problem appears when I scroll up in the RecyclerView for a while and then I go back again to the map.
When I do that the app crashes and shows this error:
java.lang.IllegalArgumentException: No view found for id 0x1 (unknown) for fragment SupportMapFragment{606864b #0 id=0x1}
Many thanks and kind regards, Marcel.
I assume bindMapRow
is called at onBindViewHolder
.
The reason for the error IllegalArgumentException: No view found for id
is that holder.mapContainer
and frame
is not attached to the RecyclerView/Activity yet during onBindViewHolder
(thus the view not found error).
To guarantee the view is already attached to RecyclerView/Activity before calling FragmentTransaction.replace
, listen to onViewAttachedToWindow
instead.
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/
这篇关于Android谷歌地图动态片段RecyclerView持有人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!