简体   繁体   中英

Material Drawer setOnDrawerNavigationListener used to run method when drawer is being opened

I am using mikepenz's Material Drawer . What I try to achieve is implement a listener for the drawer so when it is being opened I can make a request to the API and update the header with the data returned from API. I have two questions: 1. When I try to implement the listener it does not get triggered. My listener looks like this:

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. Taking into consideration that I have a custom header which I populate like this:

    final View sidebarHeader = factory.inflate(R.layout.sidebar_header, null); TextView username = (TextView) sidebarHeader.findViewById(R.id.username); 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()); 

What is the best way to update the info? Is this approach the right one?

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

Or should I regenerate the header?

Drawer format:

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();

Thank you for your time!

Update:

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()));

To listen for onDrawerOpened simply add a OnDrawerListener via the DrawerBuilder https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/DrawerBuilder.java#L1150

This listener provides all the features you need

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

If you have a custom header, updating the views directly is the correct approach. So your first approach seems fine.

Based on @mikepenez's answer I have found a solution:

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();

Sadly updating the header did not work with updateName() or updateIcon() methods, but I stumbled upon the fact that I simply have to change the username and email TextViews from my custom header layout, like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM