禁用导航抽屉中的项目

时间:2013-08-29 23:04:36

标签: android navigation actionbarsherlock

在我的导航抽屉中我有一个问题,当它在第一个位置加载任何东西都会使应用程序崩溃,所以我的解决方法是将其设置为一个字符串,改为“(appname)Free”或“( appname)Premium“,取决于是否购买了高级升级。

我希望这个不可点击,因为它目前能够被点击但没有任何反应。理想情况下,这将是一个子菜单或标题,但我无法弄清楚如何实现它。以下是我的代码的摘录:

public class myClass extends SherlockActivity implements
    OnItemSelectedListener {

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;


@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);

    mTitle = mDrawerTitle = getTitle();

    if (mIsPremium == true) {
        mPlanetTitles = getResources().getStringArray(
                R.array.planets_array_prem);
    } else {
        mPlanetTitles = getResources()
                .getStringArray(R.array.planets_array);
    }

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // set a custom shadow that overlays the main content when the drawer
    // opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);
    // set up the drawer's list view with items and click listener
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mPlanetTitles));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
    mDrawerLayout, /* DrawerLayout object */
    R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
    R.string.drawer_open, /* "open drawer" description for accessibility */
    R.string.drawer_close /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle(mTitle);
            supportInvalidateOptionsMenu(); // creates call to
                                            // onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle(mDrawerTitle);
            supportInvalidateOptionsMenu(); // creates call to
                                            // onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        selectItem(0);
    }

}

/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements
        ListView.OnItemClickListener {

    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        switch (position) {
        case 0:
            /*
             * Toast.makeText( getApplicationContext(),
             * "case 0 -  A Activity.", Toast.LENGTH_LONG).show(); Intent
             * c0 = new Intent(getBaseContext(),
             * activity.class); startActivity(c0);
             */break;
        case 1:
            Toast.makeText(getApplicationContext(),
                    "case 1 - B Activity", Toast.LENGTH_LONG).show();
            Intent c1 = new Intent(getBaseContext(),
                    ActivityB.class);
            startActivity(c1);
            break;
        default:
        }
    }
}

public void selectItem(int position) {
    switch (position) {
    case 0:
        // setContentView(R.layout.main);
        break;
    case 1:
        setContentView(R.layout.main);
        break;
    default:
    }
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getSupportActionBar().setTitle(mTitle);
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}

我不使用片段,我打算通过点击并移动到新活动来实现导航抽屉。我正在寻找解决我的主要问题的方法,但欢迎任何设计技巧。

谢谢

修改

在CommonsWare的帮助下,我开发了这个:但我的应用程序没有任何不同。我希望第一次(情况0(位置0))被禁用。

public class MyArrayAdapter extends ArrayAdapter<String> {

    public MyArrayAdapter(Context context, int position) {
        super(context, 0);
    }

    public boolean areAllItemsEnabled() {
        return false;
    }

    public boolean isEnabled(int position) {
        if (position == 0) {
            return false;
        } else {
            return true;
        }
    }

}

1 个答案:

答案 0 :(得分:6)

正如您所知,由于您已经read the answer,因此您需要将new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles)替换为自定义Adapter - 可能是一个ArrayAdapter<String> - 您覆盖的地方areAllItemsEnabled() {}返回falseisEnabled(),以便根据需要返回truefalse

另见: