【问题标题】:How to InfoWindowAdapter and onMArkerClick in Fragment如何在 Fragment 中使用 InfoWindowAdapter 和 onMArkerClick
【发布时间】:2015-04-24 08:27:42
【问题描述】:

在 Activity 类中我有这个代码:

    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));

【问题讨论】:

  • 对不起。问题是我无法编译。 “方法 getlayoutinflater() 未定义类型 InfoInstalacionesEncontradasFragment.MyInfoWindowsAdapter”和“方法 startActionMde (InstalacionesEncontradasFragment.ActionBarCallback) 未定义类型 InfoInstalacionesEncontradasFragment”

标签: android dictionary fragment


【解决方案1】:

简短的回答是: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 以更灵活的方式彼此可能会更好。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 2020-04-09
    • 2020-06-24
    • 1970-01-01
    相关资源
    最近更新 更多