0

I used the material drawer library to build the drawer. Instead of displaying a list of users(num 4 and 5), I want to display a list of objects that belong to user ( for example :CARS)in the header drawer. In fact, only one user logs into the application (num 1) and the selected object that belong to him(like : one of his cars) is showing in num #2 and 3...

by click on each object ,the profile pic(num 1) will not change or replace by choosed object picture.

here is image of what i want.

pic

in this URL @mikepenz explain about it but i didnt get it at all.

here is my class :

public class MikePenz {
  private static Drawer drawer;
  private static AccountHeader accountHeader;
  AccountHeader.OnAccountHeaderSelectionViewClickListener mOnAccountHeaderSelectionViewClickListener;
  View mAccountSwitcherArrow;
  Activity activity;




  public static void mikepenz(final Activity activity, Toolbar toolbar) {

    //after oncreate and before items start
    AccountHeader headerResult = new AccountHeaderBuilder()

      .withActivity(activity)
      .withHeaderBackground(R.mipmap.car)


      .addProfiles(
        new ProfileDrawerItem().withName("pejman ghorbany").withEmail("pejman.gh66@gmail.com").withIcon(R.drawable.ic_build_black_24dp),
        new ProfileDrawerItem().withName("sepahdar ghorbani").withEmail("sepahdar.gh41@gmail.com").withIcon(R.drawable.ic_check_box_black_24dp)
      )
      .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
        @Override
        public boolean onProfileChanged(View view, IProfile profile, boolean current) {
          return false;
        }
      })

      .build();
    //after on create and before items end

    //if you want to update the items in a later time it recommanded to keep it in a variable;

    PrimaryDrawerItem drawerItem1 = new PrimaryDrawerItem().withIdentifier(10).withName("home");
    PrimaryDrawerItem drawerItem2 = new PrimaryDrawerItem().withIdentifier(2).withName("settin2");
    PrimaryDrawerItem drawerItem3 = new PrimaryDrawerItem().withIdentifier(3).withName("settin3");
    SecondaryDrawerItem drawerItems1 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(6).withName("s6");

    //Create the drawer and remember the 'drawer' result object
     drawer = new DrawerBuilder()
      .withActivity(activity)
      .withAccountHeader(headerResult)
      .withToolbar(toolbar)



      //create the account header
      .addDrawerItems(
        new SecondaryDrawerItem().withName("setting").withIcon(R.drawable.ic_build_black_24dp),
        drawerItem1,
        new SecondaryDrawerItem().withName("build").withSubItems(drawerItem2, drawerItems1).withIcon(R.drawable.ic_build_black_24dp),
        drawerItem3
      )
      .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
        @Override
        public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
          switch (position) {
            case 1:
              break;
            case 2:
              Toast.makeText(activity, "clicked 2 ", Toast.LENGTH_SHORT).show();
              break;
            case 3:
              Toast.makeText(activity, "clicked 3 ", Toast.LENGTH_SHORT).show();
              break;
            case 4:
              Toast.makeText(activity, "clicked 4 ", Toast.LENGTH_SHORT).show();
              break;
            case 5:
              Toast.makeText(activity, "clicked 5 ", Toast.LENGTH_SHORT).show();
              break;

          }
          return true;
        }
      })

      .build();
  }

}
4

1 回答 1

1

The MaterialDrawer library's AccountHeaderView was specifically crafted to simplify the usecase of having the requirement to implement an account switcher into applications. This follows the general idea of the click on the header opening those profiles.

In case you will only have a single profile, and no way to switch these. You can most likely make use of the ProfileSettingDrawerItem's to fill this list.

In a simplified form this could look like the following demo:

headerView = AccountHeaderView(this).apply {
        activeProfile = profile
        attachToSliderView(binding.slider)
        addProfiles(
                //don't ask but google uses 14dp for the add account icon in gmail but 20dp for the normal icons (like manage account)
                ProfileSettingDrawerItem().apply { nameText = "Car Y" },
                ProfileSettingDrawerItem().apply { nameText = "Car X"; iconicsIcon = GoogleMaterial.Icon.gmd_settings }
        )
        withSavedInstance(savedInstanceState)
    }

(Please note that this sample code is base don the latest release (v8.x.y) of the library)

Beyond that you can use the API to modify the text shown in for example sec 3

headerView.selectionSecondLine = "Car X
于 2021-02-05T09:41:33.187 回答