2017-03-18 2 views
0

Ich habe die NavigationDrawer Vorlage in meiner Anwendung verwendet, aber die Funktionen der Absicht nicht ordnungsgemäß funktioniert. Ich habe den Navigations-Listener bereits in der Klassendeklaration implementiert, aber die Absichtsnavigation funktioniert nicht. Dies ist der Ort, wo ich beim ersten Mal stecken geblieben bin. Ich habe viele Websites durchsucht, aber in meiner Anwendung ist nichts passiert. Kannst du mir helfen? Ich habe einige zusätzliche Kommentare bitte nicht halten es fürNavigationDrawer Intent Funktion funktioniert nicht

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; 

/** * Extra die die Einführung Absicht hinzuzufügen, dass Benutzer angeben, von der Benachrichtigung kommt (zur * zeigen nicht den aktuellen Monat, aber der letzte) */

 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> 

Antwort

0

in Ihrer onNavigationItemSelected() Methode Rückkehr true statt false

auch die ID stellen Sie sicher, im Menü der Navigationsleiste vorhanden.

app:menu="@menu/menu_navigation" 

Verwendung dieser Code.

@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

nicht funktioniert @ rafsa –

+0

hat Ihr 'onNavigationItemSelected()' Methode aufgerufen? legte ein Protokoll .. und auch sauber bauen das Projekt .. manchmal kleine Änderungen werden nicht erkannt in 'sofort run' – rafsanahmad007

+0

Ich weiß nicht, wie Sie log ..guide mich setzen –

Verwandte Themen