2017-09-14 83 views
0

我正在使用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; 
      } 
     }); 
  • 考慮到我有一個自定義標題,該標題我填充這樣的:

    最後查看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()); 
    
  • 什麼是更新信息的最佳方式?這種方法是正確的嗎?

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

    回答

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

    要聽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