嘿伙计们,我不久前阅读了这个线程并尝试复制它,因为我在我的应用程序中使用了多个活动。我在新NavigationDrawerActivity.java
中复制了导航抽屉的所有代码,如下所示:
package info.androidhive.navigationdrawer.activity;
import android.content.Intent;
import android.os.Handler;
import android.support.annotation.LayoutRes;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import info.androidhive.navigationdrawer.R;
import info.androidhive.navigationdrawer.fragment.CameraFragment;
import info.androidhive.navigationdrawer.fragment.ExposeDetailFragment;
import info.androidhive.navigationdrawer.fragment.HomeFragment;
import info.androidhive.navigationdrawer.fragment.MyAccountFragment;
import info.androidhive.navigationdrawer.fragment.SettingsFragment;
public class NavigationDrawerActivity extends AppCompatActivity {
public DrawerLayout drawerLayout;
public NavigationView navigationView;
public DrawerLayout drawer;
public View navHeader;
public TextView txtName;
public TextView txtWebsite;
public Toolbar toolbar;
// index to identify current nav menu item
public static int navItemIndex = 0;
// tags used to attach the fragments
public static final String TAG_HOME = "home";
public static final String TAG_CAMERA = "camera";
public static final String TAG_MOVIES = "movies";
public static final String TAG_MY_ACCOUNT = "myaccoount";
public static final String TAG_SETTINGS = "settings";
public static String CURRENT_TAG = TAG_HOME;
// toolbar titles respected to selected nav menu item
public String[] activityTitles;
// flag to load home fragment when user presses back key
public boolean shouldLoadHomeFragOnBackPress = true;
public Handler mHandler;
@Override
public void setContentView(@LayoutRes int layoutResID) {
drawerLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_navigation_drawer,null);
getLayoutInflater().inflate(layoutResID,drawerLayout,true);
super.setContentView(drawerLayout);
//DrawerContent here
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mHandler = new Handler();
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
// Navigation view header
navHeader = navigationView.getHeaderView(0);
txtName = (TextView) navHeader.findViewById(R.id.name);
txtWebsite = (TextView) navHeader.findViewById(R.id.webseite);
// load toolbar titles from string resources
activityTitles = getResources().getStringArray(R.array.nav_item_activity_titles);
// load nav menu header data
loadNavHeader();
// initializing navigation menu
setUpNavigationView();
}
/***
* Load navigation menu header information
* like background image, profile image
* name, website, notifications action view (dot)
*/
private void loadNavHeader() {
// name, website
txtName.setText("Ravi Tamada");
txtWebsite.setText("www.androidhive.info");
}
/***
* Returns respected fragment that user
* selected from navigation menu
*/
public void loadHomeFragment() {
// selecting appropriate nav menu item
selectNavMenu();
// set toolbar title
setToolbarTitle();
// if user select the current navigation menu again, don't do anything
// just close the navigation drawer
if (getSupportFragmentManager().findFragmentByTag(CURRENT_TAG) != null) {
drawer.closeDrawers();
return;
}
// Sometimes, when fragment has huge data, screen seems hanging
// when switching between navigation menus
// So using runnable, the fragment is loaded with cross fade effect
// This effect can be seen in GMail app
Runnable mPendingRunnable = new Runnable() {
@Override
public void run() {
// update the main content by replacing fragments
Fragment fragment = getHomeFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
android.R.anim.fade_out);
fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
fragmentTransaction.commitAllowingStateLoss();
}
};
// If mPendingRunnable is not null, then add to the message queue
if (mPendingRunnable != null) {
mHandler.post(mPendingRunnable);
}
/*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Click action
Intent intent = new Intent(NavigationDrawerActivity.this, NewExposeActivity.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Ein neues Exposé erstellen", Toast.LENGTH_LONG).show();
}
});*/
//Closing drawer on item click
drawer.closeDrawers();
// refresh toolbar menu
invalidateOptionsMenu();
}
public Fragment getHomeFragment() {
switch (navItemIndex) {
case 0:
// home
HomeFragment homeFragment = new HomeFragment();
return homeFragment;
case 1:
// expose List
HomeFragment homeFragment1 = new HomeFragment();
return homeFragment1;
case 2:
//Camera fragment
CameraFragment cameraFragment = new CameraFragment();
return cameraFragment;
case 3:
// notifications fragment
MyAccountFragment myAccountFragment = new MyAccountFragment();
return myAccountFragment;
case 4:
// settings fragment
SettingsFragment settingsFragment = new SettingsFragment();
return settingsFragment;
default:
return new HomeFragment();
}
}
public void setToolbarTitle() {
getSupportActionBar().setTitle(activityTitles[navItemIndex]);
}
public void selectNavMenu() {
navigationView.getMenu().getItem(navItemIndex).setChecked(true);
}
public void setUpNavigationView() {
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()) {
//Replacing the main content with ContentFragment Which is our Inbox View;
case R.id.home:
navItemIndex = 0;
CURRENT_TAG = TAG_HOME;
break;
case R.id.nav_camera:
navItemIndex = 1;
CURRENT_TAG = TAG_CAMERA;
break;
case R.id.nav_movies:
navItemIndex = 2;
CURRENT_TAG = TAG_MOVIES;
break;
case R.id.nav_my_account:
navItemIndex = 3;
CURRENT_TAG = TAG_MY_ACCOUNT;
break;
case R.id.nav_settings:
navItemIndex = 4;
CURRENT_TAG = TAG_SETTINGS;
break;
case R.id.nav_rss:
startActivity( new Intent(NavigationDrawerActivity.this, RSSFeedActivity.class));
drawer.closeDrawers();
return true;
case R.id.nav_login:
startActivity( new Intent(NavigationDrawerActivity.this, LoginActivity.class));
drawer.closeDrawers();
return true;
default:
navItemIndex = 0;
}
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) {
menuItem.setChecked(false);
} else {
menuItem.setChecked(true);
}
menuItem.setChecked(true);
loadHomeFragment();
return true;
}
});
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) {
@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawer.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessary or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawers();
return;
}
// This code loads home fragment when back key is pressed
// when user is in other fragment than home
if (shouldLoadHomeFragOnBackPress) {
// checking if user is on other navigation menu
// rather than home
if (navItemIndex != 0) {
navItemIndex = 0;
CURRENT_TAG = TAG_HOME;
loadHomeFragment();
return;
}
}
super.onBackPressed();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@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.
int id = item.getItemId();
if (id == R.id.add_expose) {
Toast.makeText(getApplicationContext(), "Ein neues Exposé soll erstellt werden!", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
这里是activity_navigation_drawer.xml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
这是menu
资源文件activity_navigation_drawer.xml
:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/nav_home" />
<item
android:id="@+id/nav_camera"
android:icon="@mipmap/ic_list_black_24dp"
android:title="@string/nav_photos" />
<item
android:id="@+id/nav_movies"
android:icon="@mipmap/ic_camera_alt_black_24dp"
android:title="@string/nav_movies" />
<item
android:id="@+id/nav_my_account"
android:icon="@mipmap/ic_person_black_24dp"
android:title="Mein Account" />
<item
android:id="@+id/nav_settings"
android:icon="@mipmap/ic_settings_black_24dp"
android:title="@string/nav_settings" />
</group>
<item android:title="Other">
<menu>
<item
android:id="@+id/nav_rss"
android:title="News"
android:icon="@mipmap/ic_rss_feed_black_24dp" />
<item
android:id="@+id/nav_login"
android:title="@string/action_logout"
android:icon="@mipmap/ic_lock_black_24dp" />
</menu>
</item>
</menu>
它工作得很好,因为当我单击导航中的项目时没有任何反应。对我做错了什么有什么建议吗?
您可以在此处找到整个项目。
感谢您的帮助。
首先,发布一个关于你如何创建菜单项的参考或代码,你是在资源菜单文件中创建它还是简单地加载一个数组,如果你使用的是数组,那么我建议你使用菜单xml来创建菜单项。
像这样:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
android:icon="@drawable/ic_new_game"
android:title="@string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="@+id/help"
android:icon="@drawable/ic_help"
android:title="@string/help" />
</menu>
还要对代码进行相应的更改,检查https://developer.android.com/guide/topics/ui/menus.htmlhttps://developer.android.com/training/implementing-navigation/nav-drawer.html