You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义mikepenz Material Drawer:替换用户列表为用户所属对象展示

解决Material Drawer侧边栏头部替换问题

Got it, let's sort this out for you. The core idea is to separate your fixed user profile from the selectable vehicle profiles in the AccountHeader—this way clicking a vehicle won't replace the user's avatar, while still updating the displayed vehicle info. Here's how to adjust your code step by step:

关键修改思路

  • Mark the current logged-in user as a non-selectable fixed profile to lock their avatar in place (no replacement when clicking other items)
  • Replace the original user list (spots 4 and 5) with the user's vehicle list as selectable profiles
  • Handle the profile selection event to update the displayed vehicle details (spots 2 and 3) while keeping the user's avatar intact

修改后的完整代码

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) {
        // 1. Create fixed logged-in user profile (non-selectable to protect avatar)
        ProfileDrawerItem currentUserProfile = new ProfileDrawerItem()
                .withName("pejman ghorbany") // Spot 1: User name
                .withEmail("pejman.gh66@gmail.com")
                .withIcon(R.drawable.ic_build_black_24dp) // Spot 1: User avatar
                .withSelectable(false); // Critical: Prevent this profile from being selected

        // 2. Create vehicle list to replace original user profiles (spots 4 & 5)
        List<IProfile> vehicleProfiles = new ArrayList<>();
        vehicleProfiles.add(new ProfileDrawerItem()
                .withName("Vehicle A") // Shows in spot 2 when selected
                .withEmail("License:京A12345") // Shows in spot 3 when selected
                .withIcon(R.mipmap.car_icon_1));
        vehicleProfiles.add(new ProfileDrawerItem()
                .withName("Vehicle B")
                .withEmail("License:京B67890")
                .withIcon(R.mipmap.car_icon_2));

        // 3. Build AccountHeader with fixed user + vehicle list
        AccountHeader headerResult = new AccountHeaderBuilder()
                .withActivity(activity)
                .withHeaderBackground(R.mipmap.car)
                .addProfiles(currentUserProfile) // Add fixed user first
                .addProfiles(vehicleProfiles) // Add vehicle list next
                .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                    @Override
                    public boolean onProfileChanged(View view, IProfile profile, boolean current) {
                        // 4. Handle vehicle selection: update display but keep user avatar
                        if (profile instanceof ProfileDrawerItem && !profile.equals(currentUserProfile)) {
                            // Update header to show vehicle info, reuse user's avatar
                            accountHeader.updateProfile(new ProfileDrawerItem()
                                    .withName(profile.getName()) // Update spot 2
                                    .withEmail(profile.getEmail()) // Update spot 3
                                    .withIcon(currentUserProfile.getIcon())); // Keep spot 1 avatar
                        }
                        return false;
                    }
                })
                .build();
        accountHeader = headerResult; // Save reference for later updates

        // Original DrawerItem setup remains unchanged
        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");

        // Build the drawer
        drawer = new DrawerBuilder()
                .withActivity(activity)
                .withAccountHeader(headerResult)
                .withToolbar(toolbar)
                .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();
    }
}

核心代码解释

  • Fixed User Profile: Using withSelectable(false) ensures the logged-in user's profile can't be selected, so their avatar (spot 1) never gets replaced.
  • Vehicle List Replacement: The original user profiles are swapped out for vehicle profiles, which appear in the dropdown list (spots 4 and 5).
  • Selection Handling: When a vehicle is selected, we create a new profile that uses the vehicle's name/email (for spots 2 and 3) but reuses the original user's avatar—this keeps the user's icon intact while updating the vehicle info.
  • AccountHeader Reference: Saving the headerResult lets us dynamically update the header display when a vehicle is selected.

内容的提问来源于stack exchange,提问作者Pejman Gh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 10:03:13