使用DrawerList菜单打开新活动和/或片段

时间:2017-12-26 00:35:04

标签: java android

所以我已经按照教程实现了一个侧面菜单(左边的菜单幻灯片),一切都很完美,但我不知道如何使菜单可用。我想在按钮点击时启动新活动或片段。现在它显示了4个字符串(Android,IOS,Windows,OS X,Linux)但是当我点击它们时没有任何反应?

任何帮助都会很棒!感谢。

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

}

还有这行代码,我相信我可以在哪里进行更改以使其成为可能?

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

3 个答案:

答案 0 :(得分:0)

如果您的onItemClickListener正在运行,那么您可以使用position int来检测要启动的Activity。

如果你使osArray成为一个实例变量,那么你应该能够使用

访问字符串本身
String title = osArray[position];

你可以做一系列if / else if / else语句来选择一个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
}

答案 1 :(得分:0)

您需要为监听器添加一些代码以用于mDrawerList(您的上一个代码段)。

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

此代码将启动一个新活动,将NewActivity替换为您要启动的活动名称,并将MainActivity替换为当前活动名称。确保您保留.this.class扩展名。

所以你的Listener方法看起来与此类似。

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

您可能需要添加一些选择语句来确定要启动的活动。例如按下奶酪开始斯蒂尔顿活动。您可以使用位置变量来确定要启动的活动。

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

这些答案依赖于osArray已公开,在类的顶部声明它,以便所有子方法都可以访问它。

答案 2 :(得分:0)

试试这个:

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

    }
});