如何在 Fragment 中使用 InfoWindowAdapter 和 onMArkerClick

How to InfoWindowAdapter and onMArkerClick in Fragment

在 Activity class 我有这个代码:

    class MyInfoWindowAdapter implements InfoWindowAdapter{

    private  View view;

    public MyInfoWindowAdapter(){

        view = getLayoutInflater().inflate(R.layout.custom_info_globo_marker,null);

    }

    //implemets metod of MyInfoWidowAdapter
    @Override
    public View getInfoContents(Marker marker) {

            TextView tvTitle = ((TextView)view.findViewById(R.id.title));
            tvTitle.setText(marker.getTitle());
            return view;
    }

    @Override
    public View getInfoWindow(Marker marker) {
    // TODO Auto-generated method stub
    return null;
    }

}

@Override
public boolean onMarkerClick(Marker marker) {
    // TODO Auto-generated method stub

    //ActionBar Contextual
    mActionMode = InstalacionesEncontradasMostradasEnMapaActivity.this.startActionMode(new ActionBarCallBack(marker));
    mActionMode.setTitle(marker.getTitle());


    return false;
}

现在我在 Fragment 工作。我有两个错误。

这一行一个(方法未定义):

view = getLayoutInflater().inflate(R.layout.custom_info_globo_marker,null);

并在这一行中(方法启动操作模式未定义):

mActionMode = InstalacionesEncontradasMostradasEnMapaFragment.this.startActionMode(new ActionBarCallBack(marker));

简短的回答是:Activity != Fragment

为了获得 LayoutInflater 个实例,您需要一个 Context。各种选项(来自片段内部):

getActivity().getLayoutInflater().inflate(...);
LayoutInflater.from(getActivity()).inflate(...);
((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(...);

startActionMode() 的类似问题。此方法仅适用于 Activity,因此您需要在以下对象上调用它:

getActivity().startActionMode(...)

或者,如果您使用的是支持库:

((AppCompatActivity) getActivity()).startSupportActionMode(...)

(虽然以更灵活的方式彼此make the activity and fragment communicate可能会更好。)