禁用导航抽屉中的项目

use*_*871 3 navigation android 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);
}
Run Code Online (Sandbox Code Playgroud)

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

谢谢

编辑

在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;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

Com*_*are 6

正如您已经知道的那样,既然您已经阅读了答案,那么您需要将自己替换new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles)为自定义Adapter- 也许是一个自定义- ArrayAdapter<String>您可以覆盖它areAllItemsEnabled()以返回falseisEnabled()返回truefalse根据需要.

也可以看看: