材料抽屉setOnDrawerNavigationListener用于在抽屉打开时运行方法

问题描述:

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

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 username =(TextView)sidebarHeader.findViewById(R.id.username); username.setText(u.getUsername()); CircleImageView profilePic =(CircleImageView)sidebarHeader.findViewById(R.id.profilePic); ().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()); 
    
  2. 什么是更新信息的最佳方式?这种方法是正确的吗?

    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())); 
    
开始=>

基于@ 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); 

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

这听众提供添加OnDrawerListener所有你需要

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

功能

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

+0

我已经设置监听器,但我有设置我的自定义标题的电子邮件的问题,我应该用什么方法? updateBadge()或updateName()并给我的自定义标题中找到我的电子邮件元素的ID?我用我最近的代码更新了我的问题。 – Alphonse

+0

你的头文件中的东西不会被任何提供的方法更新,因为它们只更新默认的'AccountHeader'或列表项。如果您提供的是自定义视图,则只需查看视图的内容即可 – mikepenz