【问题标题】:Android Google Maps dynamic fragment in RecyclerView holderRecyclerView holder中的Android Google Maps动态片段
【发布时间】: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


【解决方案1】:

我假设bindMapRowonBindViewHolder 被调用。

错误IllegalArgumentException: No view found for id 的原因是holder.mapContainerframeonBindViewHolder 期间尚未附加到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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 2014-06-06
    • 2012-11-28
    相关资源
    最近更新 更多