【问题标题】:What is the function that can be used instead of "this" in "Fragment"?“Fragment”中可以用什么函数代替“this”?
【发布时间】:2019-12-18 16:43:35
【问题描述】:

我正在 Google map place api 中实现自动完成建议代码。 我们使用OnMapReadyCallback 作为工具。

MapView.getMapAsync(this) 函数最初用于onCreateView。但现在我将在setupAutoCompleteFragment 中使用它。但是,在MapView.getMapAsync(this) 中,由于this,它没有被编译。有什么用?

public class googlemaptab extends Fragment implements OnMapReadyCallback {
    MapView mapview;
    Button kakaobutton;
    public static googlemaptab newInstance(){
        return new googlemaptab();
    }
    public googlemaptab() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_googlemaptab, container, false);
        kakaobutton = (Button)view.findViewById(R.id.kakaobutton);

        mapview = (MapView)view.findViewById(R.id.google_map_view);
        setupAutoCompleteFragment();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        kakaobutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //클릭하면 카카오maptab으로 이동하겠다.
                ((MainActivity)getActivity()).replaceFragment(kakaomaptab.newInstance());
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        mapview.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapview.onResume();
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if(mapview != null)
        {
            mapview.onCreate(savedInstanceState);
        }

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        LatLng SEOUL = new LatLng(37.56, 126.97);
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(SEOUL);
        markerOptions.title("서울");
        markerOptions.snippet("수도");
        googleMap.addMarker(markerOptions);
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(SEOUL));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(13));
    }

    private void setupAutoCompleteFragment() {

        PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)getActivity().
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                mapview.getMapAsync(this);
            }

            @Override
            public void onError(Status status) {
                Log.e("Error", status.getStatusMessage());
            }
        });
    }
}

【问题讨论】:

  • mapview.getMapAsync(this)中可以用什么代替“this”;

标签: java android android-studio


【解决方案1】:

这样可以做得更好

private void setupAutoCompleteFragment(OnMapReadyCallback instance) {

        PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)getActivity().
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                mapview.getMapAsync(instance);
            }

            @Override
            public void onError(Status status) {
                Log.e("Error", status.getStatusMessage());
            }
        });
    }

别忘了用新的函数签名更新您的onCreateView,如下所示:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_googlemaptab, container, false);
        kakaobutton = (Button)view.findViewById(R.id.kakaobutton);

        mapview = (MapView)view.findViewById(R.id.google_map_view);
        setupAutoCompleteFragment(this);
        return view;
    }

【讨论】:

    【解决方案2】:

    在这种情况下,您可以使用Classname.this

    例如:如果你的片段名称是HomeFragment 那么,

    HomeFragment.this
    

    如果这仍然不起作用,那么您可以覆盖 onViewCreated() 函数并在 onViewCreated() 内调用 mapView.getMapAsync(this)

    如果需要,请查看link

    【讨论】:

    • 那是代码的味道! HomeFragment 很容易通过这种方式泄露。
    • 如果它泄露了HomeFragment,那么可能会覆盖onViewCreated 方法并在那里编写代码。
    【解决方案3】:

    您是否尝试过 mapview.getMapAsync(getActivity());而不是 mapview.getMapAsync(this);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 2011-09-03
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多