地图碎片无法片段刷新后进行修改 [英] Map Fragment couldn't be modified after Fragment Reload

查看:251
本文介绍了地图碎片无法片段刷新后进行修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我有一个的活动抽屉式导航 HomeFragment

HomeFragment 包含两个嵌套的片段(子片段): SupportMapFragment ActionFragment

HomeFragment contains two nested fragments (child fragments): SupportMapFragment and ActionFragment.

我宣布GoogleMap对象的 SMAP 的作为静态的 HomeFragment ,因此它可以访问和修改嵌套的片段 ActionFragment

I declare the GoogleMap object sMap as static in HomeFragment, so it can be accessed and modified by the nested fragment ActionFragment.

一切正常,在第一次运行的罚款。地图可以从内部的 ActionFragment 进行操作。但是当HomeFragment除去和重新装载在稍后的时间点,在地图上只能从HomeFragment(父片段)改性而不是从ActionFragment(嵌套片段)了。

Everything works fine in the first run. The map can be manipulated from within the ActionFragment. But when the HomeFragment is removed and reloaded at a later point of time, the map could only be modified from the HomeFragment (parent fragment) and not from the ActionFragment (nested fragment) anymore.

我没能理解为什么一些在一审时的作品片段重新加载不起作用。下面是我一直保持到最低限度更容易理解codeS。

I am failing to understand why something that works in the first instance doesn't work when the fragment is reloaded. Below are the codes which I have kept to minimum for easier understanding.

HomeFragment.java (家长片段)

public class HomeFragment extends Fragment {

    private static GoogleMap sMap;
    public static GoogleMap getMap() {
        return sMap;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate layout
        return inflater.inflate(R.layout.f_home, container, false);
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        // Obtain the GoogleMap object
        try {
            if (sMap == null) {
                sMap = ((SupportMapFragment) getActivity().getSupportFragmentManager()
                        .findFragmentById(R.id.home_map)).getMap();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Load child fragment
        if (savedInstanceState == null) {
            Fragment _actionFragment = new ActionFragment();
            FragmentTransaction _ft = getChildFragmentManager().beginTransaction();
            _ft.replace(R.id.action_container_bottom, _actionFragment);
            _ft.commit();
        }
    }


    @Override
    public void onDestroyView(){
        super.onDestroyView();

        // Remove the map fragment to prevent errors on the next load
        if(sMap != null){
            try {
                getActivity().getSupportFragmentManager()
                            .beginTransaction()
                            .remove(getActivity().getSupportFragmentManager()
                                    .findFragmentById(R.id.home_map))
                            .commit();

                sMap = null;
            } catch (Exception e){}
        }
    }
}

f_home.xml (父片段的布局)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/home_map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout
        android:id="@+id/action_container_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

ActionFragment.java (嵌套或子片段)

public class ActionFragment extends Fragment {
    private static GoogleMap sMap = HomeFragment.getMap();


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

        return inflater.inflate(R.layout.f_action, container, false);
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        // check if map is created successfully or not
        if (sMap != null) {
            sMap.clear();
            sMap.setPadding(0, 0, 0, 300);
            sMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            sMap.setBuildingsEnabled(false);
            sMap.setMyLocationEnabled(false);
            sMap.getUiSettings().setRotateGesturesEnabled(false);
            sMap.getUiSettings().setTiltGesturesEnabled(false);
            sMap.getUiSettings().setZoomControlsEnabled(false);
            sMap.moveCamera(CameraUpdateFactory.zoomTo(12));
        }
    }
}

任何帮助将是因为我这个挣扎了很长一段时间非常AP preciated。谢谢!

Any help would be immensely appreciated as I am struggling with this for a long time. Thank you!

推荐答案

糟糕的设计。
这是错误的,保持静止无功用于存储地图参考。假设你有一个里面有2 HomeFragment或HomeFragment几项活动。它不会工作。

Bad design. It's wrong to keep static var for storing reference to a map. Suppose you have two HomeFragment or several activities that have HomeFragment inside. It won't work.

我认为你应该使用标签来发现碎片。或者更好的创造活动的一些接口,实现您的片段之间的通信。反正你不能使用静态字段为你做的方式映射。

I think you should use tags to find fragments. Or better create some interface in activity that implements communication between your fragments. Anyway you must not use static field for a map in a way you do.

您可以使用此code:

You can use this code:

((SupportMapFragment) getActivity().getSupportFragmentManager()
                        .findFragmentById(R.id.home_map)).getMap();

让每一个你需要时间映射。

to get map every time you need.

这篇关于地图碎片无法片段刷新后进行修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆