简体   繁体   中英

Open new activity and/or Fragment using DrawerList Menu

So I've followed a tutorial to implement a side menu (Menu slides in from left), everything works perfectly but I'm not sure how to make the menu usable. I want to start either a new activity or Fragment on button click. Right now it shows the 4 Strings (Android, IOS, Windows, OS X, Linux) but when I click them nothing happens?

Any assistance would be great! Thanks.

  private void addDrawerItems() {
    String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" };
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
    mDrawerList.setAdapter(mAdapter);
}

private void setupDrawer() {

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.string.drawer_open, R.string.drawer_close) {

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {

            super.onDrawerOpened(drawerView);
            getSupportActionBar().setTitle("Navigation!");
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            getSupportActionBar().setTitle(mActivityTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()

        }
    };


    mDrawerToggle.setDrawerIndicatorEnabled(true);
    mDrawerLayout.setDrawerListener(mDrawerToggle);

}

@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.

    //noinspection SimplifiableIfStatement
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);

}

There is also this line of code as well that I believe is where I may make the changes to make this possible?

mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
        }
    });

If your onItemClickListener is working, then you can use the position int to detect which Activity to launch.

If you make your osArray an instance variable, then you should be able to access the Strings themselves with

String title = osArray[position];

You could do a series of if/else if/else statements to select an Activity:

if (position == 0) {
    // Launch first activity
}
else if (position == 1) {
    // Launch second activity
}
else {
    // Launch an activity if not in position 0 or 1
}

You need to add some code to the Listener for mDrawerList (your last snippet).

Intent intent = new Intent(MainActivity.this, NewActivity.class);
startActivity(intent);

This code will start a new Activity, replace NewActivity with the name of your activity to start, and replace MainActivity with the current activity name. Ensure you keep the .this and .class extensions.

So your Listener method would look similar to this.

mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(MainActivity.this, "TESTING", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(MainActivity.this, NewActivity.class);
        startActivity(intent);
    }
});

You may want to add some selection statements to determine which activity to start. eg Cheese is pressed start the stilton activity. You can use the position variable to determine which activity to start.

if (osArray[position] == 1){
    Intent intent = new Intent(MainActivity.this, NewActivityOne.class);
}
if (osArray[position] == 2){
    Intent intent = new Intent(MainActivity.this, NewActivityTwo.class);
}
startActivity(intent);

These answers rely on osArray been made public, declare it at the top of your class so all child methods can access it.

Try this:

mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch(position) {
            case 0:     // Pos (0) of your osArray 
                startActivity(new Intent(this, AndroidActivity.class));
                break
            case 1:     // Pos (1) of your osArray... etc. 
                startActivity(new Intent(this, IosActivity.class));
                break;
            case 2:
                startActivity(new Intent(this, WindowsActivity.class));
                break;
            case 3:
                startActivity(new Intent(this, OsxActivity.class));
                break;
            case 4:
                startActivity(new Intent(this, LinuxActivity.class));
                break;
            default:
                Toast.makeText(MainActivity.this, "No such activity.", Toast.LENGTH_SHORT).show();
            break;
        }

    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM