我在我的應用程序中使用自定義導航器抽屜。對於切換活動,我爲每個按鈕使用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);
}
});
發佈您的代碼片段 – Anil
你的意思是「每個活動」? NavDrawer應該切換*一個Activity的片段* –
我必須在每個活動中放置點擊監聽器才能使菜單正常工作。 @ cricket_007 –