0

我正在使用mikepenz 的 Material Drawer。我试图实现的是为抽屉实现一个侦听器,因此当它被打开时,我可以向 API 发出请求并使用从 API 返回的数据更新标头。我有两个问题: 1. 当我尝试实现监听器时,它不会被触发。我的听众看起来像这样:

result.setOnDrawerNavigationListener(new Drawer.OnDrawerNavigationListener(){
            @Override
            public boolean onNavigationClickListener(View view) {
                //If the drawer is not yet opened but at the end of the action it will be
                if (!result.isDrawerOpen())
                {
                    getCurrentUser(savedInstanceState);
                    return true;
                }
                else
                    onBackPressed();
                return false;
            }
        });
  1. 考虑到我有一个自定义标题,我填充如下:

    最终视图 sidebarHeader = factory.inflate(R.layout.sidebar_header, null); TextView 用户名 = (TextView) sidebarHeader.findViewById(R.id.username); 用户名.setText(u.getUsername()); CircleImageView profilePic = (CircleImageView) sidebarHeader.findViewById(R.id.profilePic); Picasso.with(this).load(BuildConfig.BASE_API_URL + profilePictureUrl).fit().placeholder(R.drawable.default_user_icon).error(R.drawable.default_user_icon).into(profilePic);

        TextView email = (TextView) sidebarHeader.findViewById(R.id.email);
        email.setText(u.getEmail());
    

更新信息的最佳方式是什么?这种方法正确吗?

result.updateName(R.id.username, new StringHolder(response.body().getUsername()));
                String profilePictureUrl = response.body().getProfilePicture();
                CircleImageView profilePic = (CircleImageView) findViewById(R.id.profilePic);
                Picasso.with(getApplicationContext()).load(BuildConfig.BASE_API_URL + profilePictureUrl).fit().placeholder(R.drawable.default_user_icon).error(R.drawable.default_user_icon).into(profilePic);
                drawer.updateIcon(R.id.profilePic, new ImageHolder(profilePictureUrl));

或者我应该重新生成标题?

抽屉格式:

result = new DrawerBuilder()
                .withActivity(this)
                .withHeader(sidebarHeader)
                .withToolbar(toolbar)
                .addDrawerItems(
                        new PrimaryDrawerItem().withIdentifier(0).withName(R.string.dashboard).withIcon(FontAwesome.Icon.faw_tachometer),
                        new PrimaryDrawerItem().withIdentifier(1).withName(R.string.point_of_sale).withIcon(FontAwesome.Icon.faw_file_text_o),
                        new ExpandableDrawerItem().withName(R.string.ecommerce).withIcon(FontAwesome.Icon.faw_shopping_cart).withSubItems(
                                new SecondaryDrawerItem().withIdentifier(2).withName(R.string.shops)
                        ),
                        new PrimaryDrawerItem().withIdentifier(3).withName(R.string.clients).withIcon(FontAwesome.Icon.faw_briefcase),
                        new PrimaryDrawerItem().withIdentifier(4).withName(R.string.invoices).withIcon(FontAwesome.Icon.faw_list_alt),
                        new PrimaryDrawerItem().withIdentifier(5).withName(R.string.payment_requests).withIcon(R.drawable.payment_request),
                        new ExpandableDrawerItem().withName(R.string.catalog).withIcon(FontAwesome.Icon.faw_folder_open).withSubItems(
                                new SecondaryDrawerItem().withIdentifier(6).withName(R.string.products),
                                new SecondaryDrawerItem().withIdentifier(7).withName(R.string.categories)
                        ),
                        new PrimaryDrawerItem().withIdentifier(8).withName(R.string.settings).withIcon(FontAwesome.Icon.faw_cog),
                        new ExpandableDrawerItem().withName(R.string.reports).withIcon(FontAwesome.Icon.faw_list_ol).withSubItems(
                                new SecondaryDrawerItem().withIdentifier(9).withName(R.string.transactions),
                                new SecondaryDrawerItem().withIdentifier(10).withName(R.string.orders)
                        )
                )
                .addStickyDrawerItems(new PrimaryDrawerItem().withIdentifier(11).withName(R.string.sign_out).withIcon(FontAwesome.Icon.faw_lock))
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                        // do something with the clicked item :D
                        if (drawerItem != null) {
                            Fragment fragment = null;
                            switch ((int) drawerItem.getIdentifier()){
                                case 0:
                                    fragment = new DashboardFragment();
                                    break;
                                case 8:
                                    fragment = new SettingsFragment();
                                    break;
                                case 11:
                                    Intent i = new Intent(getApplicationContext(), SplashScreenActivity.class);
                                    MainProvider.sharedInstance().logOut(MainActivity.this);
                                    Toast.makeText(MainActivity.this, "Logged Out", Toast.LENGTH_SHORT).show();
                                    startActivity(i);
                                    break;

                            }
                            if(fragment != null){
                                getSupportFragmentManager().beginTransaction().replace(R.id.mainFragment,(android.support.v4.app.Fragment) fragment, Integer.toString((int) drawerItem.getIdentifier())).addToBackStack(null).commit();
                            }
                        }
                        return false;
                    }
                })
                .withSavedInstance(savedInstanceState)
                .build();

感谢您的时间!

更新:

result.updateName(R.id.username, new StringHolder(response.body().getData().getUsername()));
                String profilePictureUrl = response.body().getData().getSettings().getProfilePicture();
                CircleImageView profilePic = (CircleImageView) findViewById(R.id.profilePic);
                Picasso.with(getApplicationContext()).load(BuildConfig.BASE_API_URL + profilePictureUrl).fit().placeholder(R.drawable.default_user_icon).error(R.drawable.default_user_icon).into(profilePic);
                result.updateIcon(R.id.profilePic, new ImageHolder(profilePictureUrl));
                result.updateName(R.id.email, new StringHolder(response.body().getData().getEmail()));
4

2 回答 2

0

要收听onDrawerOpened只需OnDrawerListener通过DrawerBuilder https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/DrawerBuilder.java#L1150添加

此侦听器提供您需要的所有功能

public interface OnDrawerListener {
    /**
     * @param drawerView
     */
    void onDrawerOpened(View drawerView);

    /**
     * @param drawerView
     */
    void onDrawerClosed(View drawerView);

    /**
     * @param drawerView
     * @param slideOffset
     */
    void onDrawerSlide(View drawerView, float slideOffset);
}

如果您有自定义标题,则直接更新视图是正确的方法。所以你的第一种方法似乎很好。

于 2017-09-19T06:22:14.700 回答
0

根据@mikepenez 的回答,我找到了解决方案:

new DrawerBuilder()
                .withActivity(this)
                .withHeader(sidebarHeader)
                .withToolbar(toolbar)
                .addDrawerItems(
                        new PrimaryDrawerItem().withIdentifier(0).withName(R.string.dashboard).withIcon(FontAwesome.Icon.faw_tachometer),
                        new PrimaryDrawerItem().withIdentifier(1).withName(R.string.point_of_sale).withIcon(FontAwesome.Icon.faw_file_text_o),
                        new ExpandableDrawerItem().withName(R.string.ecommerce).withIcon(FontAwesome.Icon.faw_shopping_cart).withSubItems(
                                new SecondaryDrawerItem().withIdentifier(2).withName(R.string.shops)
                        ),
                        new PrimaryDrawerItem().withIdentifier(3).withName(R.string.clients).withIcon(FontAwesome.Icon.faw_briefcase),
                        new PrimaryDrawerItem().withIdentifier(4).withName(R.string.invoices).withIcon(FontAwesome.Icon.faw_list_alt),
                        new PrimaryDrawerItem().withIdentifier(5).withName(R.string.payment_requests).withIcon(R.drawable.payment_request),
                        new ExpandableDrawerItem().withName(R.string.catalog).withIcon(FontAwesome.Icon.faw_folder_open).withSubItems(
                                new SecondaryDrawerItem().withIdentifier(6).withName(R.string.products),
                                new SecondaryDrawerItem().withIdentifier(7).withName(R.string.categories)
                        ),
                        new PrimaryDrawerItem().withIdentifier(8).withName(R.string.settings).withIcon(FontAwesome.Icon.faw_cog),
                        new ExpandableDrawerItem().withName(R.string.reports).withIcon(FontAwesome.Icon.faw_list_ol).withSubItems(
                                new SecondaryDrawerItem().withIdentifier(9).withName(R.string.transactions),
                                new SecondaryDrawerItem().withIdentifier(10).withName(R.string.orders)
                        )
                )
                .addStickyDrawerItems(new PrimaryDrawerItem().withIdentifier(11).withName(R.string.sign_out).withIcon(FontAwesome.Icon.faw_lock))
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                        // do something with the clicked item :D
                        if (drawerItem != null) {
                            Fragment fragment = null;
                            switch ((int) drawerItem.getIdentifier()){
                                case 0:
                                    fragment = new DashboardFragment();
                                    break;
                                case 8:
                                    fragment = new SettingsFragment();
                                    break;
                                case 11:
                                    Intent i = new Intent(getApplicationContext(), SplashScreenActivity.class);
                                    MainProvider.sharedInstance().logOut(MainActivity.this);
                                    Toast.makeText(MainActivity.this, "Logged Out", Toast.LENGTH_SHORT).show();
                                    startActivity(i);
                                    break;

                            }
                            if(fragment != null){
                                getSupportFragmentManager().beginTransaction().replace(R.id.mainFragment,(android.support.v4.app.Fragment) fragment, Integer.toString((int) drawerItem.getIdentifier())).addToBackStack(null).commit();
                            }
                        }
                        return false;
                    }
                })
                    .withOnDrawerListener(new Drawer.OnDrawerListener() {
                        @Override
                        public void onDrawerOpened(View drawerView) {
                            getCurrentUser();
                        }

                        @Override
                        public void onDrawerClosed(View drawerView) {

                        }

                        @Override
                        public void onDrawerSlide(View drawerView, float slideOffset) {
                        }
                    })

                    .withSavedInstance(savedInstanceState)
                .build();

遗憾的是,更新标头不适用于 updateName() 或 updateIcon() 方法,但我偶然发现我只需从自定义标头布局更改用户名和电子邮件 TextViews,如下所示:

View sidebarHeader = drawer.getHeader();
//Update header username
TextView username = (TextView) sidebarHeader.findViewById(R.id.username);
username.setText(response.body().getData().getUsername());
//Update header email
TextView email = (TextView) sidebarHeader.findViewById(R.id.email);
email.setText(response.body().getData().getEmail());
//Update header profile picture
String profilePictureUrl = response.body().getData().getSettings().getProfilePicture();
CircleImageView profilePic = (CircleImageView) sidebarHeader.findViewById(R.id.profilePic);
Picasso.with(getApplicationContext()).load(BuildConfig.BASE_API_URL + profilePictureUrl).fit().placeholder(R.drawable.default_user_icon).error(R.drawable.default_user_icon).into(profilePic);
于 2017-09-19T11:20:48.197 回答