4

我通过创建 BaseActivity 为所有活动准备了一个抽屉:

public class BaseActivity extends AppCompatActivity implements 
    NavigationView.OnNavigationItemSelectedListener {

    private static final String SELECTED_ITEM_ID = "selected_id";
    public DrawerLayout mDrawerLayout;
    public Toolbar mToolBar;
    public NavigationView mDrawer;
    public ActionBarDrawerToggle mdrawerToggle;

    private int mSelectedId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState != null){
            mSelectedId = savedInstanceState.getInt(SELECTED_ITEM_ID);
        }
        Log.d("aoi", "save instance "+String.valueOf(mSelectedId));
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
        Intent intent = null;
        if(mSelectedId != menuItem.getItemId()){
            mSelectedId = menuItem.getItemId();
            Log.d("aoi", "selected item "+String.valueOf(mSelectedId));

            mDrawerLayout.closeDrawer(GravityCompat.START);
            switch (menuItem.getItemId()){
                case R.id.agency_menu_item:
                    intent = new Intent(this, AgencyActivity.class);
                    startActivity(intent);
                    break;
            }
        }
        return false;
    }

     public void initDrawer(){
        mToolBar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(mToolBar);
        mDrawer = (NavigationView) findViewById(R.id.main_drawer);
        mDrawer.setNavigationItemSelectedListener(this);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_parent);
        mdrawerToggle = new ActionBarDrawerToggle(
                this,
                mDrawerLayout,
                mToolBar,
                R.string.drawer_open,
                R.string.drawer_close);
        mDrawerLayout.setDrawerListener(mdrawerToggle);
        // indicator based on whether the drawerlayout is in open or closed
        mdrawerToggle.syncState();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(SELECTED_ITEM_ID, mSelectedId);
    }
}

并将由我的活动扩展

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

 public class AgencyActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agency);
        initDrawer();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_agency, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

但是我的问题是如何禁用抽屉中的项目,如果它已经显示活动?例如:我单击抽屉上的“代理”项目,当我已经在该活动中时,我将无法再次单击它,以避免打开AgencyActivity.

我尝试使用onSaveInstanceState保存所选项目的值并在我再次尝试选择它时进行比较,但没有运气。我认为当通过意图调用新活动时它会回到零。

我目前正在为此使用Android 设计支持库

4

1 回答 1

3

单击时禁用菜单:

menu.getItem(menuItem.getItemId()).setEnabled(false);

然后在必要时使用 启用它setEnabled(true);

于 2015-06-29T13:40:54.360 回答