我将此抽屉布局用作导航抽屉。如何将项目单击侦听器设置为其中的项目。例如,我在抽屉布局中有Home,Maps和About,我想在每个项目点击上打开新片段。
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mActionBarDrawerToggle;
private ScrimInsetsFrameLayout mScrimInsetsFrameLayout;
private void init_navigator(){
// Navigation Drawer
mDrawerLayout = (DrawerLayout) findViewById(R.id.main_activity_DrawerLayout);
mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.primaryDark));
mScrimInsetsFrameLayout = (ScrimInsetsFrameLayout) findViewById(R.id.main_activity_navigation_drawer_rootLayout);
mActionBarDrawerToggle = new ActionBarDrawerToggle
(
this,
mDrawerLayout,
toolbar,
R.string.navigation_drawer_opened,
R.string.navigation_drawer_closed
)
{
@Override
public void onDrawerSlide(View drawerView, float slideOffset)
{
// Disables the burger/arrow animation by default
super.onDrawerSlide(drawerView, 0);
}
};
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
if (getSupportActionBar() != null)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
mActionBarDrawerToggle.syncState();
// Navigation Drawer layout width
int possibleMinDrawerWidth = UtilsDevice.getScreenWidth(this) -
UtilsMiscellaneous.getThemeAttributeDimensionSize(this, android.R.attr.actionBarSize);
int maxDrawerWidth = getResources().getDimensionPixelSize(R.dimen.navigation_drawer_max_width);
mScrimInsetsFrameLayout.getLayoutParams().width = Math.min(possibleMinDrawerWidth, maxDrawerWidth);
// Set the first item as selected for the first time
getSupportActionBar().setTitle(R.string.toolbar_title_home);
}
答案 0 :(得分:0)
你应该有这样的东西:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {}
和
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id){
case R.id.nav_dashboard:
return true;
case R.id.nav_maps:
return true;
case R.id.nav_user_management:
return true;
case R.id.nav_view:
return true;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main_activity_DrawerLayout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
在创建新项目时尝试导航抽屉活动以获得更好的导航抽屉。