Update Material Header Drawer Header

I use the recently provided Google Support Library to add a navigation box to my application. In the header of the navigation box, I define a CircleImageView to set the pic user profile, its name under it.

enter image description here

All this information is first determined by the user on the first launch of the application, so when mainActivity starts, the image and name are loaded into the header. But I give the user the opportunity to change them inside the application. If the user goes to the profile fragment and selects a new image as the pic profile or if he changes the name, these parameters will not be updated in the header of the navigation box until the application is closed and loaded again.

This is how I defined it in mainActivity:

public class MainActivity extends AppCompatActivity implements DrawerLayout.DrawerListener { private NavigationView navView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); .... navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { menuItem.setChecked(true); displayFragment(menuItem.getItemId()); drawerLayout.closeDrawers(); return true; } }); } 

There is no need to implement DrawerLayout.DrawerListener, but as you can see at the top, I did this to try to update the title in real time. They implement the following methods:

 @Override public void onDrawerSlide(View drawerView, float slideOffset) { } @Override public void onDrawerOpened(View drawerView) { try { ContextWrapper cw = new ContextWrapper(getApplicationContext()); File directory = cw.getDir("profile", Context.MODE_PRIVATE); File mypath = new File(directory, "thumbnail.png"); Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(mypath)); thumbview.setImageBitmap(bitmap); username = mPreferences.getString("NAME", null); nameview.setText(username); } catch (FileNotFoundException e) { Log.e("LOAD_IMAGE", e.getMessage(), e); thumbview.setImageResource(R.drawable.default_thumbnail); } } @Override public void onDrawerClosed(View drawerView) { } @Override public void onDrawerStateChanged(int newState) { } 

In the onDrawerOpened () method, I used the same code that I use in onCreate to load the pic profile and name. This should update this parameter every time the box is opened, but it doesn’t do this, still does not update the header until I close and open the application.

+6
source share
1 answer

In onCreate, I need to define the following code in order to be able to update the navigation box header in real time:

 public class MainActivity extends AppCompatActivity implements DrawerLayout.DrawerListener { private DrawerLayout drawerLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); drawerLayout.setDrawerListener(this); ... 
+1
source

Source: https://habr.com/ru/post/989340/


All Articles