material 抽屉 mikepenz 自定义

material drawer mikepenz customize

我使用 material 抽屉库来构建抽屉。我不想显示用户列表(编号 4 和 5),而是想在 header 抽屉中显示属于用户(例如:CARS)的 object 列表。 事实上,只有一个用户登录到应用程序 (num 1) 并且属于他的选定 object(例如:他的一辆汽车)显示在 num #2 和 3...

通过单击每个 object,个人资料图片(数字 1)不会更改或替换为所选 object 图片。

这是我想要的图像。

this URL @mikepenz 中对此进行了解释,但我根本不明白。

这是我的 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();
  }

}

MaterialDrawer 库的 AccountHeaderView 专门用于简化需要在应用程序中实施帐户切换器的用例。这遵循了单击 header 打开这些配置文件的一般思路。

如果您只有一个配置文件,并且无法切换这些配置文件。您很可能可以使用 ProfileSettingDrawerItem 来填充此列表。

在简化形式中,这可能类似于以下演示:

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)
    }

(请注意,此示例代码基于库的最新版本 (v8.x.y))

除此之外,您还可以使用 API 修改例如 sec 3

中显示的文本
headerView.selectionSecondLine = "Car X