2017-03-18 45 views
0

我在我的應用程序中使用了NavigationDrawer模板,但意圖的功能無法正常工作。我已經在類聲明中實現了導航監聽器,但仍然無法使用意向導航。這是我第一次陷入困境的地方。我搜索了很多網站,但沒有發生在我的應用程序中。你可以幫我嗎?我添加了一些額外的意見,請不要認爲這是NavigationDrawer意圖功能不起作用

public class MainActivity extends AppCompatActivity 
     implements ViewPager.OnPageChangeListener { 
    /** 
    * Extra to add the the launch intent to specify that user comes from the notification (used to 
    * show not the current month but the last one) 
    */ 
    public static final String FROM_NOTIFICATION_EXTRA = "fromNotif"; 

    /** 
    * List of first date of each month available 
    */ 
    private List<Date> dates; 
    /** 
    * TextView that displays the name of the month 
    */ 
    private TextView monthTitleTv; 
    /** 
    * Button to go the previous month 
    */ 
    private Button previousMonthButton; 
    /** 
    * Button to go the next month 
    */ 
    private Button nextMonthButton; 
    /** 
    * ViewPager used to display each month in a Fragment 
    */ 
    private ViewPager pager; 
    /** 
    * The current {@link #pager} position 
    */ 
    private int selectedPosition; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     final ProgressBar progressBar = (ProgressBar) findViewById(R.id.monthly_report_progress_bar); 
     final View content = findViewById(R.id.monthly_report_content); 
     monthTitleTv = (TextView) findViewById(R.id.monthly_report_month_title_tv); 
     previousMonthButton = (Button) findViewById(R.id.monthly_report_previous_month_button); 
     nextMonthButton = (Button) findViewById(R.id.monthly_report_next_month_button); 
     pager = (ViewPager) findViewById(R.id.monthly_report_view_pager); 

     previousMonthButton.setText("<"); 
     nextMonthButton.setText(">"); 
     previousMonthButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (selectedPosition > 0) { 
        selectPagerItem(selectedPosition - 1, true); 
       } 
      } 
     }); 
     nextMonthButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (selectedPosition < dates.size() - 1) { 
        selectPagerItem(selectedPosition + 1, true); 
       } 
      } 
     }); 
     UIHelper.removeButtonBorder(previousMonthButton); 
     UIHelper.removeButtonBorder(nextMonthButton); 
     // Load list of monthly asynchronously since it can take time 
     new AsyncTask<Void, Void, List<Date>>() { 
      @Override 
      protected List<Date> doInBackground(Void... params) { 
       return DateHelper.getListOfMonthsAvailableForUser(MainActivity.this); 
      } 

      @Override 
      protected void onPostExecute(List<Date> dates) { 
       if (isFinishing()) { 
        return; 
       } 

       MainActivity.this.dates = dates; 

       configureViewPager(); 

       progressBar.setVisibility(View.GONE); 
       content.setVisibility(View.VISIBLE); 
      } 
     }.execute(); 
    } 

    private void configureViewPager() { 
     pager.setOffscreenPageLimit(0); 
     pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { 
      @Override 
      public Fragment getItem(int position) { 
       return new MonthlyReportFragment(dates.get(position)); 
      } 

      @Override 
      public int getCount() { 
       return dates.size(); 
      } 
     }); 
     pager.addOnPageChangeListener((ViewPager.OnPageChangeListener) this); 


     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 


     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
      @Override 
      public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
       int id = item.getItemId(); 
       switch (id) { 
        case R.id.nav_camera: 
         Intent i = new Intent(MainActivity.this, Monthly_ExpenseEdit_activity.class); 
         startActivity(i); 
         break; 
        case R.id.nav_gallery: 
         Intent i1 = new Intent(MainActivity.this, ExpenseEditActivity.class); 
         startActivity(i1); 
         break; 
       } 
       return false; 
      } 
     }); 
    } 

/** 
    * Extra to add the the launch intent to specify that user comes from the notification (used to 
    * show not the current month but the last one) 
    */ 



    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      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; 
    } 




    private void selectPagerItem(int position, boolean animate) 
    { 
     pager.setCurrentItem(position, animate); 
     onPageSelected(position); 
    } 

    @Override 
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

    } 
/** 
    * Extra to add the the launch intent to specify that user comes from the notification (used to 
    * show not the current month but the last one) 
    */ 
    @Override 
    public void onPageSelected(int position) 
    { 
     selectedPosition = position; 

     Date date = dates.get(position); 

     monthTitleTv.setText(DateHelper.getMonthTitle(this, date)); 

     // Last and first available month 
     boolean last = position == dates.size() - 1; 
     boolean first = position == 0; 

/** * 額外添加的推出意圖指定用戶來自的通知(用於 *不顯示當月但最後一個) */

 nextMonthButton.setEnabled(!last); 
     nextMonthButton.setTextColor(ContextCompat.getColor(this, last ? R.color.monthly_report_disabled_month_button : android.R.color.white)); 
     previousMonthButton.setEnabled(!first); 
     previousMonthButton.setTextColor(ContextCompat.getColor(this, first ? R.color.monthly_report_disabled_month_button : android.R.color.white)); 
    } 

    @Override 
    public void onPageScrollStateChanged(int state) { 

    } 


} 

XML: 

<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" /> 
    <FrameLayout 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" 
     tools:context=".view.MonthlyReportActivity"> 

     <ProgressBar android:id="@+id/monthly_report_progress_bar" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:indeterminate="true" /> 

     <LinearLayout android:id="@+id/monthly_report_content" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="47dp" 
       android:orientation="horizontal" 
       android:paddingLeft="10dp" 
       android:paddingRight="10dp" 
       android:gravity="center_vertical" 
       android:background="@color/primary_dark"> 
       <Button 
        android:id="@+id/test" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
       /> 

       <Button android:id="@+id/monthly_report_previous_month_button" 
        android:layout_width="50dp" 
        android:layout_height="wrap_content" 
        android:textColor="@android:color/white" 
        android:textSize="24dp" 
        android:background="@drawable/calendar_month_switcher_button_drawable" /> 

       <TextView android:id="@+id/monthly_report_month_title_tv" 
        android:layout_width="0dip" 
        android:layout_height="wrap_content" 
        android:layout_weight="1.0" 
        android:textSize="21dp" 
        android:textColor="@android:color/white" 
        android:textAllCaps="true" 
        android:gravity="center" /> 

       <Button android:id="@+id/monthly_report_next_month_button" 
        android:layout_width="50dp" 
        android:layout_height="wrap_content" 
        android:textColor="@android:color/white" 
        android:textSize="24dp" 
        android:background="@drawable/calendar_month_switcher_button_drawable" /> 

      </LinearLayout> 

      <android.support.v4.view.ViewPager 
       android:id="@+id/monthly_report_view_pager" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"/> 

     </LinearLayout> 



    </FrameLayout> 

</android.support.v4.widget.DrawerLayout> 

回答

0

onNavigationItemSelected()方法的返回true而不是false

同時確保ID存在於導航抽屜菜單中。

app:menu="@menu/menu_navigation" 

使用此代碼:

@Override 
public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
    int id = item.getItemId(); 
    switch (id) { 
     case R.id.nav_camera: 
      Intent i = new Intent(MainActivity.this, Monthly_ExpenseEdit_activity.class); 
      startActivity(i); 
      break; 
     case R.id.nav_gallery: 
      Intent i1 = new Intent(MainActivity.this, ExpenseEditActivity.class); 
      startActivity(i1); 
      break; 
    } 
    return true; //change here 
} 
+0

沒有工作@ rafsa –

+0

沒叫你'onNavigationItemSelected()'方法?把log.and也乾淨的建立項目..有時小變化檢測不到'即時運行' – rafsanahmad007

+0

我不知道如何設置日誌..指導我 –