I just open activity but not click into any items. But function withOnDrawerItemClickListener run fist time ?
You may be setting the drawer to open position 0 on launch, which is triggering the listener. If you need more help maybe post how you've added the drawer.
new DrawerBuilder().withActivity(this).build();
final PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(1).withName("Create new order ");
final PrimaryDrawerItem item2 = new PrimaryDrawerItem().withIdentifier(2).withName("Created order");
PrimaryDrawerItem item3 = new PrimaryDrawerItem().withIdentifier(3).withName("Tutorials");
PrimaryDrawerItem item4 = new PrimaryDrawerItem().withIdentifier(4).withName("Your profile");
PrimaryDrawerItem item5 = new PrimaryDrawerItem().withIdentifier(5).withName("About us");
PrimaryDrawerItem item6 = new PrimaryDrawerItem().withIdentifier(6).withName("Version");
PrimaryDrawerItem item7 = new PrimaryDrawerItem().withIdentifier(7).withName("Settings");
PrimaryDrawerItem item8 = new PrimaryDrawerItem().withIdentifier(8).withName("Logout");
AccountHeader headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withHeaderBackground(R.drawable.background_home)
.addProfiles(
new ProfileDrawerItem().withName("Minh").withEmail("Tr峄媙h V膬n Minh")//.withIcon(myUri)
)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
@Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
return false;
}
})
.build();
final Drawer result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.addDrawerItems(
item1,
item2,
item3,
item4,
item5,
item6,
item7,
item8
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
// do something with the clicked item :D // # On create not click it auto run
if(drawerItem.getIdentifier() ==1)
{
Intent intent = new Intent(getApplicationContext(), HandleMapsActivity.class);
getApplicationContext().startActivity(intent);
}
if(drawerItem.getIdentifier() ==2)
{
Intent intent = new Intent(getApplicationContext(), CreatedOrderActivity.class);
getApplicationContext().startActivity(intent);
}
return false;
}
})
.build();
result.setSelection(1);
result.setSelection(item2);
result.setSelection(1, true);
item1.withBadge("19").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_create_new));
item2.withBadge("5").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_created));
item3.withBadge("5").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_tutorials));
item4.withBadge("19").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_your_profile));
item5.withBadge("5").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_about));
item6.withBadge("5").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_version));
item7.withBadge("19").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_settings));
item8.withBadge("5").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_logout));
item1.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
Intent intent = new Intent(getApplicationContext(), HandleMapsActivity.class);
getApplicationContext().startActivity(intent);
return false;
}
});
item2.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
if(drawerItem.getIdentifier() == 2)
{
Intent intent = new Intent(getApplicationContext(), CreatedOrderActivity.class);
getApplicationContext().startActivity(intent);
}
return true;
}
});
result.openDrawer();
result.closeDrawer();
result.getDrawerLayout();
Is that your actual implementation? You have a few instance where you've built the drawer and didn't use it, and you won't need to open then close the drawer immediately.
On the top of your formatted code portion, you can see result.setSelection(1, true);, which sets the selection and has true which will emit a click event
Thank you. I got it, but how I can set selection for each item when item clicked ? It not automation select anymore. ??
final Drawer result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.addDrawerItems(
item1,
item2,
item3,
item4,
item5,
item6,
item7,
item8
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
// do something with the clicked item :D
if(drawerItem.getIdentifier() ==1)
{
Intent intent = new Intent(getApplicationContext(), HandleMapsActivity.class);
getApplicationContext().startActivity(intent);
return true;
}
if(drawerItem.getIdentifier() == 2)
{
Intent intent = new Intent(getApplicationContext(), CreatedOrderActivity.class);
getApplicationContext().startActivity(intent);
return true;
}
return false;
}
})
.build();
item1.withBadge("19").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_create_new));
item2.withBadge("5").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_created));
item3.withBadge("5").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_tutorials));
item4.withBadge("19").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_your_profile));
item5.withBadge("5").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_about));
item6.withBadge("5").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_version));
item7.withBadge("19").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_settings));
item8.withBadge("5").withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_green_900)).withIcon(getResources().getDrawable(R.drawable.ic_logout));
@trinhvanminh2009 return false in the listener. true means the event was consumed, and no further action is done in the drawer itself. This is mentioned in the JavaDoc of that method.
@mikepenz Thanks . I return true or return false always have a problem. After startActivity(intent) it always choose selection(item1). I'm using this class is abstract and others class extends from this class. I can't use
result.setSelection(item1) after startActivity. How I can fix it.
Thank you.
I know when I startActivity(Intent) then new class will refresh this. That why it always selection(item1). Can you give me an idea to fix it ?
@trinhvanminh2009 Typically, the drawer is attached to the activity and the inner contents are managed by fragments. Perhaps you should look at that rather than launching a new activity every time. If you do choose to stick with the activity, maybe pass the position index into the next activity and then set the drawer position to that value when it loads. Look up intent extras for that.
Edit: I now see that a lot of the code you posted matches that of the sample app. Keep in mind that the purpose of that app is to showcase many drawer styles, which is why it launches the activities. Your app likely has a different purpose.
So , I should use fragment or use intent.putExtra. I have 2 options ?
@trinhvanminh2009 I suggest you look into fragments. That way you only have one drawer and you don't have to worry about the selection. Have a look at how to switch fragments, which you will do so when you receive a drawer click
Yeah . I used fragment for google maps and dialog fragment. But this way I never try before. Thank you so much . I'll try that right now .
@AllanWang thanks for answering :)
@trinhvanminh2009 many people have already achieved it by using activities. It's simply more complicated as the Drawer can't properly keep the state through the different activities, and you have to handle the positioning, ...
Most people who did this created a BaseActivity which had the drawer code and so on.
Also remember that you can select a drawer item without calling the listener. (Just see the JavaDoc)
And you can always handle more complex tasks by handling some things with the listener.
@mikepenz Thanks for support. I'm using the Drawer in BaseActivity. But it's not working perfect like me think. Maybe my code have some problem. I think using Fragment is better idea. Using BaseActivity also good , but how to change activity not using intent or how to set Selection in onDrawerItemClickListener ?
Most helpful comment
@trinhvanminh2009 Typically, the drawer is attached to the activity and the inner contents are managed by fragments. Perhaps you should look at that rather than launching a new activity every time. If you do choose to stick with the activity, maybe pass the position index into the next activity and then set the drawer position to that value when it loads. Look up intent extras for that.
Edit: I now see that a lot of the code you posted matches that of the sample app. Keep in mind that the purpose of that app is to showcase many drawer styles, which is why it launches the activities. Your app likely has a different purpose.