从另一种方法加载时,在sherlock操作栏上设置seticon?

时间:2014-08-22 01:44:29

标签: android actionbarsherlock

我有布局java内容地图,我已经在操作栏的右侧添加了新项目,项目显示在加载时加载地图时加载..

enter image description here

我希望在地图完成加载后将menuitem更改为图标 这里是我的代码,用于加载项目。

 public boolean onCreateOptionsMenu(Menu menu) {
        boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;
        menu.add("Icon").setActionView(R.layout.loading_lingkaran_kecil)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        return true;
    }



@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapv2_main_activity);
        googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                public void onMapLoaded() {
                    Toast.makeText(getApplicationContext(), "map has loaded", Toast.LENGTH_SHORT).show();
        //here to make items stop show loading and change icon,but nothing happend with this code
                    getSupportActionBar().getTabAt(0).setIcon(R.drawable.ico_member);


                }
             });
}

提前致谢

1 个答案:

答案 0 :(得分:0)

您需要有一个布尔标志来检查是否已完成加载,并在删除旧图标时将invalidateOptionsMenu调用onCreateOptionsMenu并更换为OptionMenu中的新图标。

<强>样品:

boolean doneLoading = false; //a global instance of the flag
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if(!doneLoading)
        menu.add("Icon").setActionView(R.layout.loading_lingkaran_kecil).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    else
    {
        menu.removeItem(R.layout.loading_lingkaran_kecil); //will remove the old icon after the loading is done
        menu.add("Icon").setActionView(R.layout.you_new_layout_after_loading).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }
    return true;
}

在您的onMapLoadedCallback中,您需要刷新操作栏中的菜单项,将标志设置为true。

googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                public void onMapLoaded() {
                    Toast.makeText(getApplicationContext(), "map has loaded", Toast.LENGTH_SHORT).show();

                    doneLoading = true; //set to true means it is done loading
                    invalidateOptionsMenu(); //refresh the optionsmenu

                }
             });