繁体   English   中英

Google Maps Android API - 带片段的自定义信息窗口

[英]Google Maps Android API - Custom InfoWindow with Fragment

编辑:这个问题的要点是:How do I inflate a Fragment in a Google Maps InfoWindow in Android。 请参阅下面的尝试。 谢谢!


我的应用程序中的片段上有一个谷歌地图,我能够在我点击屏幕的地方得到一个子片段来膨胀,但我需要子片段保持在原始触摸位置,所以我决定尝试通过在触摸点创建标记并自动打开信息窗口来使用信息窗口。

但是,我在将片段附加到 InfoWindow 时遇到问题。 这是我到目前为止所拥有的:

    private View fragmentLayout;
    private RelativeLayout fragmentMapLayout;
    private MapView mv;
    private GoogleMap gm;
    private RelativeLayout fragmentContainer;
    private FrameLayout fragmentContainerLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        fragmentLayout = inflater.inflate(R.layout.fragment_map, container, false);
        fragmentMapLayout = (RelativeLayout) fragmentLayout.findViewById(R.id.map_fragment_layout);

        mv = (MapView) fragmentLayout.findViewById(R.id.mapview);
        mv.onCreate(savedInstanceState);

        mv.getMapAsync(this);

        return fragmentLayout;
    }

    @Override
    public void onMapReady(GoogleMap map) {
        gm = map;
        gm.setMyLocationEnabled(true);

        gm.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng point) {
                float pX = (float) point.latitude;
                float pY = (float) point.longitude;

                gm.addMarker(new MarkerOptions()
                    .position(new LatLng(pX, pY))
                    .alpha(0.0f)
                    .icon(BitmapDescriptorFactory.fromResource(R.mipmap.t_box01))
                    .title("title")).showInfoWindow();

            }
        });
        gm.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                return null;
            }
            @Override
            public View getInfoContents(Marker marker) {
                infoWindowLayout = getActivity().getLayoutInflater().inflate(R.layout.fragment_container, null);
                fragmentContainerLayout = (FrameLayout) infoWindowLayout.findViewById(R.id.fragment_container);

                    FragmentTransaction transaction = getFragmentManager().beginTransaction();
                    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    transaction.add(fragmentContainerLayout.getId(), new FragmentTwo(), "fragment_id");
                    transaction.commit();

                return fragmentContainerLayout;
            }
        });
    }

XML 片段_容器.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:orientation="vertical"
    android:layout_width="100dp"
    android:layout_height="100dp">

</FrameLayout>

显然,一些无关的代码也丢失了,但这是涉及这个问题的代码。 我收到此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.example.exampleapp, PID: 18936
                                                                   java.lang.IllegalArgumentException: No view found for id 0x7f0f00d1 (com.example.exampleapp:id/fragment_container) for fragment FragmentTwo{2ad9a637 #5 id=0x7f0f00d1 fragment_id}
                                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
                                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                                       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                                       at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
                                                                       at android.os.Handler.handleCallback(Handler.java:739)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:135)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

当尝试将 FragmentTwo 直接插入到主 Fragment 中时,这种膨胀 FragmentTwo 的相同方法起作用了,但是当尝试将它插入到 InfoWindow 中时,情况有所不同。

我不确定为什么它找不到已经在 xml 中的视图的 ID。

确保您在setContentView()中指定了正确的布局。 在您的情况下,传入FragmentTransaction.add()的 ID R.id.fragment_container必须是setContentView()中指定的布局的子项。

一个可能的问题是使用getFragmentManager() ,在调用片段时尝试使用getChildFragmentManager

这是片段的有用链接: https://developer.android.com/reference/android/app/Fragment.html#onActivityCreated(android.os.Bundle)

请参阅将 Android 片段添加到 InfoWindow 内容

从适配器返回的视图不是实时视图,而是转换为位图,因此与此视图关联的代码(如按钮点击)将永远不会执行。

同样的情况也适用于片段,因此您可以停止尝试,而是创建一个将 map_details 和片段布局绑定在一起的布局。 在 xml 中使用 include 可能会起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM