多个活动中的导航抽屉不起作用

时间:2015-06-05 21:46:29

标签: android android-layout android-activity navigation-drawer

我正在尝试在我的活动(主页)上使用NavigationDrawer,但未显示活动内容。

home_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/home_activity">

<Button
    android:id="@+id/like_count"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="likeCounter"
    android:background="@drawable/likecountgreen" />

</LinearLayout>

activity_drawer.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>

<ListView
    android:id="@+id/nav_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#fff"
    android:choiceMode="singleChoice" />

HomeActivity.class

public class HomeActivity extends BaseActivity {

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

BaseActivity.class

public class BaseActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {

private ListView drawerList;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private String[] options;

private TextView titleActionBar;

protected void onCreateDrawer() {
    setContentView(R.layout.activity_drawer);

    setupActionBar();

    options = getResources()
            .getStringArray(R.array.drawer_options);

    drawerLayout =
            (DrawerLayout) findViewById(R.id.drawer_layout);

    drawerList = (ListView) findViewById(R.id.nav_drawer);

    drawerList.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, options));

    drawerList.setOnItemClickListener(this);

    setupDrawer();

}

private void setupActionBar() {
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.action_bar);

    titleActionBar = (TextView) findViewById(R.id.title_action_bar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
}

private void setupDrawer() {
    mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close) {

        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            titleActionBar.setText("App");
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            titleActionBar.setText("Options");
            invalidateOptionsMenu();
        }
    };

    mDrawerToggle.setDrawerIndicatorEnabled(true);
    drawerLayout.setDrawerListener(mDrawerToggle);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    switch (position) {
        case 2:
            startActivity(new Intent(getApplicationContext(), LoginActivity.class));
            overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
            drawerLayout.closeDrawer(drawerList);
            break;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.home_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.action_search) {
        return true;
    }

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

}

我发现的是drawer_activity覆盖了home_activity。

我该如何解决?

1 个答案:

答案 0 :(得分:2)

onCreateDrawer中,您正在设置新布局,因此您在MainActivity中设置的布局不再显示。以下是解决这个问题的方法:

HomeActivity:

public class HomeActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.onCreateDrawer(R.layout.home_activity);
}
}

在您的BaseActivity中:

protected FrameLayout content;
protected void onCreateDrawer(final int layoutResID) {
    setContentView(R.layout.activity_drawer);

    content = (FrameLayout) findViewById(R.id.content_frame);
    getLayoutInflater().inflate(layoutResID, content, true);

    setupActionBar();

    options = getResources()
            .getStringArray(R.array.drawer_options);

    drawerLayout =
            (DrawerLayout) findViewById(R.id.drawer_layout);

    drawerList = (ListView) findViewById(R.id.nav_drawer);

    drawerList.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, options));

    drawerList.setOnItemClickListener(this);

    setupDrawer();

}

现在,activity_drawer布局中的FrameLayout包含HomeActivity的布局。