0

をクリックやあみんな、私はしばらく前にthisスレッドを読んで、私は私のアプリの中に複数のアクティビティを使用していますので、それを複製しようとしました。ここナビゲーションドロワーは、複数の活動のためのアイテムでの作業ではない

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>

私は私の新しい次のようになりますNavigationDrawerActivity.java内のナビゲーションドロワーのためのすべての私のコードをコピーここは電子activity_navigation_drawer.xmlmenu Ressourceファイルです。それは私が何も起こらないのナビゲーションでアイテムをクリックしているという事実に大きなまで働いて

<?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>

。私が間違っていることを示唆していますか?

あなたはプロジェクト全体hereを見つけることができます。

ありがとうございました。

+0

何も起こりません。あなたは 'R.id.nav_rss'と' R.id.nav_login'のアクティビティを開始しています – Saurabh

+0

@Saurabhだから、最初の4つを同様の活動に入れてフラグメントを使わなくてはならないと言っていますか?私は今ちょっと混乱しています – Bexx

答えて

0

最初に、メニュー項目をどのように作成するのか、リソースメニューファイルで作成するのか、単純に配列をロードするのかについてのリファレンスやコードを投稿してください。もし配列を使用しているのであれば、メニュー項目を作成します。このような

何か:あなたはどうするかコード化されていないため

<?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.html https://developer.android.com/training/implementing-navigation/nav-drawer.html

+0

私はコードスニペットを追加しました。それはメニューを追加するメニューressourceファイルです – Bexx

関連する問題