扩展导航抽屉活动

时间:2014-04-01 15:19:07

标签: android navigation-drawer

我有活动,我们称之为TestActivity。此TestActivity扩展了具有导航抽屉的Activity。在TestActivity中,我想更改NavigationDrawerToggle并用向上箭头替换它。

public class Test extends DashboardActivity {

    private DrawerLayout navDrawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        navDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        getActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.test, menu);
        return true;
    }

    @Override
    public void onBackPressed() {
        this.finish();
        super.onBackPressed();
    }

}

仪表板活动有点长,我还有更多东西,但基本上它遵循本教程:http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

1 个答案:

答案 0 :(得分:1)

我认为你想要的是一个Activity使用ActionBarDrawerToggle而另一个仍然使用DrawerLayout,但没有切换。在这种情况下,最简单的方法是拥有两个“基础”类。这是一个例子。

1)实现Activity DrawerLayout

public class BaseDrawerActivity extends Activity {

    protected DrawerLayout mDrawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_drawer_layout);

        mDrawer = ...
    }

}

2)实施Activity ActionBarDrawerToggle

public class BaseDrawerToggleActivity extends BaseDrawerActivity {

    private ActionBarDrawerToggle mDrawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDrawerToggle = ...
        mDrawer.setDrawerListener(mDrawerToggle);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

}

如果您想使用DrawerLayout 而不是切换,子类BaseDrawerActivity以及何时想要使用{/ 1}} >切换,子类Activity。因此,您的BaseDrawerToggleActivity将如下所示:

TestActivity