2017-07-29 77 views
2

我在我的應用程序中使用自定義導航器抽屜。對於切換活動,我爲每個按鈕使用setOnClickListener。但是我認爲這會消耗大量的內存,因爲每次都要在每個活動中加載相同的代碼。那麼是否有任何有效的方法來處理菜單中的點擊監聽器,如開關盒?setOnClickListener在自定義導航器抽屜中的最佳做法

我的導航項點擊代碼。

Button oneButton = (Button) findViewById(R.id.nav_one); 
    Button twoButton = (Button) findViewById(R.id.nav_two); 
    Button threeButton = (Button) findViewById(R.id.nav_three); 
    Button fourButton = (Button) findViewById(R.id.nav_four); 
    Button fiveeButton = (Button) findViewById(R.id.nav_five); 

     oneButton.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View view) { 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 

     Intent firstIntent = new Intent(getApplicationContext(), MainActivity.class); 
     startActivity(firstIntent); 
     } 
     }); 

     twoButton.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View view) { 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 

     Intent secondIntent = new Intent(getApplicationContext(), SecondActivity.class); 
     startActivity(secondIntent); 

     } 
     }); 

     threeButton.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View view) { 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 

     Intent threeButton = new Intent(getApplicationContext(), ThirdActivity.class); 
     startActivity(thirdIntent); 

     } 
     }); 

     threeButton.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View view) { 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 

     Intent fourButton = new Intent(getApplicationContext(), FouthActivity.class); 
     startActivity(fourthIntent); 

     } 
     }); 

     fourButton.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View view) { 


     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 

     Intent fiveButton = new Intent(getApplicationContext(), FifthouthActivity.class); 
     startActivity(fifthIntent); 
     } 
     }); 
+1

發佈您的代碼片段 – Anil

+0

你的意思是「每個活動」? NavDrawer應該切換*一個Activity的片段* –

+0

我必須在每個活動中放置點擊監聽器才能使菜單正常工作。 @ cricket_007 –

回答

0

使用此方法,它可以幫助你

不要忘了添加此

class YourClass ... implements View.OnClickListener 

    onCreate { 
     Button oneButton = (Button) findViewById(R.id.nav_one); 
     Button twoButton = (Button) findViewById(R.id.nav_two); 
     Button threeButton = (Button) findViewById(R.id.nav_three); 
     Button fourButton = (Button) findViewById(R.id.nav_four); 
     Button fiveeButton = (Button) findViewById(R.id.nav_five); 

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

     oneButton.setOnClickListener(this); 
     twoButton .setOnClickListener(this); 
     threeButton .setOnClickListener(this); 
     fourButton .setOnClickListener(this); 
     fiveeButton .setOnClickListener(this); 
    }//onCreate method ends here 

    @Override 
    public void onClick(View view) { 

     switch (view.getId()) 
     { 

      case R.id.nav_one: 
       drawer.closeDrawer(GravityCompat.START); 

       Intent firstIntent = new Intent(getApplicationContext(), MainActivity.class); 
       startActivity(firstIntent); 
       break; 
      case R.id.nav_two: 
       //same way other buttons also 

     } 
    } 
+0

謝謝你的回答,但我想知道是否有任何有效的方法來處理點擊監聽。我的應用程序使用大約40-50 MB的內存,我想減少它。那麼將使用開關箱幫助? –

+0

@Ganesh開關櫃不保存內存。有一個'View.OnClickListener'是...但是,這不會節省MB值 –

+0

我同意@ cricket_007 – 2017-07-29 07:11:57