Mahendran
Mahendran

Reputation: 2719

MaterialDrawer: Selection highlight & on item click listener for Account header

This question very specific to https://github.com/mikepenz/MaterialDrawer customization.

I need to customize my account header as follows:

  1. There going to be only one account at any time
  2. Clicking on header (whole area) should give me DrawerClickListener#onItemClick callback

I managed to get DrawerClickListener#onItemClick callback by redirecting the profile listing listener. But the header selection lost on orientation change. [I have set with saved instance state]. Also the onItemClick delivers null IDrawerItem as it is not part of Adapter.

Am I making it too complicated or PrimaryDrawerItem itself can be extended to look like profile item?

  1. Layout should be rendered like profile with extra fields
  2. Loading ImageHolder to be changed to load url based images

Upvotes: 1

Views: 1221

Answers (2)

Malith Ileperuma
Malith Ileperuma

Reputation: 994

Drawer Itemclick Listener

you have to declare the method inside setupNavigation. In navigation footer items , declare

List<IDrawerItem> stockyItems = new ArrayList<>();

then write withOnDrawerItemClickListener for your button

PrimaryDrawerItem primaryDrawerItem = new PrimaryDrawerItem()
            .withName("Settings")
            .withIcon(R.drawable.ic_settings)
            .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                @Override
                public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                    Intent intent = new Intent(MainActivity.this, Settings.class);
                    startActivity(intent);
                    finish();
                    return true;
                }
            });

at the end you have to pass the object

stockyItems.add(primaryDrawerItem);

Done

Upvotes: 0

mikepenz
mikepenz

Reputation: 12868

Your question is split up in multiple questions. So let me start with the first question.

  1. There going to be only one account at any time

If you build your AccountHeader you can just provide one profile. After the header was built, you can update and modify this single profile at any time, just by calling:

//create the profile
final IProfile profile = new ProfileDrawerItem().withName("Mike Penz").withEmail("[email protected]").withIcon("https://avatars3.githubusercontent.com/u/1476232?v=3&s=460").withIdentifier(100);
//build your drawer or do your logic
...
//modify the profile
profile.withName("new name");
//notify the header about the changed profile
headerResult.updateProfile(profile);
  1. Clicking on header (whole area) should give me DrawerClickListener#onItemClick callback

If you click somewhere in the AccountHeader this will trigger a OnAccountHeaderSelectionViewClickListener

.withOnAccountHeaderSelectionViewClickListener(new AccountHeader.OnAccountHeaderSelectionViewClickListener() {
   @Override
   public boolean onClick(View view, IProfile profile) {
       return false;
   }
})
  1. Layout should be rendered like profile with extra fields

Please add some more details as the question does not seem clear.

  1. Loading ImageHolder to be changed to load url based images

The Sample app contains a CustomDrawerItem which loads the icon via url: CustomUrlPrimaryDrawerItem

Upvotes: 1

Related Questions