材质抽屉mikepenz custom

问题描述

我使用了材质抽屉库来构建抽屉。我不想显示用户列表(数字4和5),而是要在标题抽屉中显示属于用户的对象列表(例如:CARS)。 实际上,只有一个用户登录到应用程序(编号1),属于他的所选对象(例如:他的一辆汽车)显示在编号2和3中……

通过单击每个对象,配置文件图片(编号1)将不会更改或替换为所选的对象图片

这是我想要的图片

pic

this URL中的

@mikepenz对此进行了解释,但我一点都没明白。

这是我的课:

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 专门用于简化需要在应用程序中实现帐户切换器的用例。这遵循点击标题打开这些配置文件的总体思路。

如果您只有一个配置文件,并且无法切换这些配置文件。您很可能可以使用 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...