RecyclerView持有者中的Android Google Maps动态片段

我正在尝试将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: No view found for id 0x1 (unkNown)
for fragment SupportMapFragment{606864b #0 id=0x1}

非常感谢和亲切的问候,
马塞尔.

解决方法

我假设在onBindViewHolder上调用了bindMapRow.

错误的原因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/

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...