3

In my Navigation Drawer I have a problem, when it loads anything in the first spot will crash the app, so my solution to this is to set it to a string that changes to say "(appname) Free" or " (appname) Premium", depending on if the premium upgrade was purchased.

I would like this unclickable as it is currently able to be clicked but nothing happens. Ideally this would be a submenu, or title, but I could not figure out how to implement that. Here is a excerpt of my code:

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

I do not use fragments, I was going to implement the navigation drawer by doing on click and moving to new activities. I am looking for a solution to my primary problem but would welcome any design tips.

Thank you

Edit

With CommonsWare's help I developed this: but nothing is different in my application. I want the first time (case 0 (position 0)) to be disabled.

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

}
4

1 回答 1

6

正如您已经知道的那样,由于您已经阅读了答案,因此您需要将您的替换new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPlanetTitles)为自定义的Adapter(可能是扩展的ArrayAdapter<String>),您可以在其中覆盖areAllItemsEnabled()以返回falseisEnabled()返回truefalse根据需要。

也可以看看:

于 2013-08-29T23:11:38.697 回答