2017-07-29 5 views
2

私のアプリでカスタムナビドロワーを使用しています。私はそれぞれのボタンにsetOnClickListenerを使用しています。しかし、毎回各アクティビティで同じコードが読み込まれるため、多くのメモリを消費する可能性があると私は考えています。そこで、スイッチケースのようなメニューのためにクリックリスナーで処理する効率的な方法はありますか?カスタムナビドロワーのsetOnClickListenerのベストプラクティス

nav-itemクリックのためのマイコード。

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は* 1つのアクティビティのフラグメントを切り替える必要があります* –

+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〜50MBのメモリを使い、それを減らしたい。だからスイッチケースのヘルプを使用しますか? –

+0

@Ganeshスイッチのケースがメモリをセーブしていません。 1つの 'View.OnClickListener'を持つことは...しかし、それはMBの価値を保存しません –

+0

私は@ cricket_007に同意します –

関連する問題