首页 > 解决方案 > 为什么 mikepenz 的 MaterialDrawer 没有在 Account Header 中显示正确的图标、名称和电子邮件?

问题描述

我在我的 Android 应用程序中使用mikepenz 的 MaterialDrawer 。该应用程序支持同时登录多个配置文件,类似于 Gmail。因此,当一个配置文件登录时,可以添加另一个配置文件,然后用户可以通过单击标题中的图标来切换活动配置文件。

我使用 Volley 从服务器异步加载个人资料图片。这意味着,当我为登录的配置文件创建列表ProfileDrawerItems时(启动应用程序时),我添加了一个默认图标。一旦 Volley 请求返回正确的图标,它就会被更新。添加新帐户时也适用相同的逻辑。

这部分工作。正确的图标显示在完整的帐户列表中(在帐户标题下方)。但在 Account Header 中,我仍然看到默认图标:

显示最新添加配置文件的默认图标的示例。这张图片显示了添加新配置文件(“Jubi”)后抽屉的外观。

成功登录后,我使用

getAccountHeader().getProfiles().add(loadProfile(token));

将新配置文件添加到帐户标题。loadProfile(Token)看起来像这样:

   /**
     * Loads and returns the profile for the given token. If a profile already exists in the map profilesByUserId for the token's userID, that profile is updated, otherwise a new profile is created and put into that map.
     * The returned profile might not yet be ready, when this method returns. Some date is loaded asynchronously: Picture, username, user's first and last name.
     *
     * @param token the token, for which the profile should be created or updated
     * @return the profile
     */
    public ProfileDrawerItem loadProfile(Token token) {
        ProfileDrawerItem profile = profilesByUserId.get(token.getUserId());
        if (profile == null) {
            profile = new ProfileDrawerItem();
            profilesByUserId.put(token.getUserId(), profile);
            profile.withIdentifier(token.getId());
            profile.withName(token.getUsername());
            profile.withIcon(R.drawable.default_profile_pic);
        }

        loadProfileData(token);
        loadProfilePic(token);
        return profile;
    }

loadProfileData(Token)loadProfilePic(Token)包含 Volley 请求。onSuccess(...)当请求成功返回时调用。

loadProfileData(Token)

   /**
     * Loads the profile data (user's name and email address) for the given token and sets it in the appropriate profile. If there is no profile for the given token, nothing happens.
     *
     * @param token token, for which the profile data should be loaded
     */
    private void loadProfileData(final Token token) {

        final ProfileDrawerItem profile = profilesByUserId.get(token.getUserId());
        if (profile == null)
            return;

        // Load name and email address
        Callback<User> callbackUser = new Callback<User>() {
            @Override
            public void onSuccess(User user) {
                profile.withName(String.format("%s %s", user.getFirstName(), user.getLastName()));
                profile.withEmail(user.getUsername());
                profile.withNameShown(true);
            }

            @Override
            public void onError(String error) {
               // not relevant
            }
        };
        loadUserById(token.getUserId(), Source.LOCAL_OVER_REMOTE, callbackUser, token);
    }

loadProfilePic(Token)

    /**
     * Loads the profile pic for the given token and sets it in the appropriate profile. If there is no profile for the given token, nothing happens.
     *
     * @param token token, for which the profile pic should be loaded
     */
    private void loadProfilePic(final Token token) {

        final ProfileDrawerItem profile = profilesByUserId.get(token.getUserId());
        if (profile == null)
            return;

        // Load profile pic
        Callback<File> callbackPic = new Callback<File>() {
            @Override
            public void onSuccess(File profilePic) {
                Bitmap bitmap = FileUtils.createBitmap(profilePic);
                // Crop center square of the bitmap
                int dimension = Math.min(bitmap.getWidth(), bitmap.getHeight());
                bitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension);
                profile.withIcon(bitmap);

            }

            @Override
            public void onError(String error) {
                // Not relevant
            }
        };
        loadProfilePicById(token.getUserId(), true, callbackPic, true);
    }

我的期望是onSuccess(File)执行后,新的个人资料图片应显示在帐户标题和标题下方的帐户列表中。但是,帐户标题中的图片、姓名和电子邮件不会更新。只有在执行切换配置文件后,getAccountHeader().setActiveProfile(token.getId())正确的数据和图片才会显示在帐户标题中。

对我来说,这看起来像是 MaterialDrawer 的问题,但我不确定。即使是这样,也许有一种解决方法。

有谁知道,在更改配置文件后,我如何让 MaterialDrawer 刷新帐户标题,或者我还能如何解决这个问题?

标签: javaandroidiconsmaterialdrawer

解决方案


推荐阅读