use of it.neokree.materialnavigationdrawer.util.MaterialDrawerLayout in project MaterialNavigationDrawer by neokree.
the class MaterialNavigationDrawer method onCreate.
@SuppressWarnings("unchecked")
@Override
protected /**
* Do not Override this method!!! <br>
* Use init() instead
*/
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources.Theme theme = this.getTheme();
TypedValue typedValue = new TypedValue();
theme.resolveAttribute(R.attr.drawerType, typedValue, true);
drawerHeaderType = typedValue.data;
theme.resolveAttribute(R.attr.rippleBackport, typedValue, false);
rippleSupport = typedValue.data != 0;
theme.resolveAttribute(R.attr.uniqueToolbarColor, typedValue, false);
uniqueToolbarColor = typedValue.data != 0;
theme.resolveAttribute(R.attr.singleAccount, typedValue, false);
singleAccount = typedValue.data != 0;
theme.resolveAttribute(R.attr.multipaneSupport, typedValue, false);
multiPaneSupport = typedValue.data != 0;
theme.resolveAttribute(R.attr.learningPattern, typedValue, false);
learningPattern = typedValue.data != 0;
theme.resolveAttribute(R.attr.drawerColor, typedValue, true);
drawerColor = typedValue.data;
theme.resolveAttribute(R.attr.defaultSectionLoaded, typedValue, true);
defaultSectionLoaded = typedValue.data;
theme.resolveAttribute(R.attr.toolbarElevation, typedValue, false);
toolbarElevation = typedValue.data != 0;
if (drawerHeaderType == DRAWERHEADER_ACCOUNTS)
super.setContentView(R.layout.activity_material_navigation_drawer);
else
super.setContentView(R.layout.activity_material_navigation_drawer_customheader);
// init Typeface
fontManager = new TypefaceManager(this.getAssets());
// init toolbar & status bar
statusBar = (ImageView) findViewById(R.id.statusBar);
toolbar = (Toolbar) findViewById(R.id.toolbar);
// init drawer components
layout = (MaterialDrawerLayout) this.findViewById(R.id.drawer_layout);
content = (RelativeLayout) this.findViewById(R.id.content);
drawer = (RelativeLayout) this.findViewById(R.id.drawer);
if (drawerHeaderType == DRAWERHEADER_ACCOUNTS) {
username = (TextView) this.findViewById(R.id.user_nome);
usermail = (TextView) this.findViewById(R.id.user_email);
userphoto = (ImageView) this.findViewById(R.id.user_photo);
userSecondPhoto = (ImageView) this.findViewById(R.id.user_photo_2);
userThirdPhoto = (ImageView) this.findViewById(R.id.user_photo_3);
usercover = (ImageView) this.findViewById(R.id.user_cover);
usercoverSwitcher = (ImageView) this.findViewById(R.id.user_cover_switcher);
userButtonSwitcher = (ImageButton) this.findViewById(R.id.user_switcher);
// set Roboto Fonts
username.setTypeface(fontManager.getRobotoMedium());
usermail.setTypeface(fontManager.getRobotoRegular());
// get and set username and mail text colors
theme.resolveAttribute(R.attr.accountStyle, typedValue, true);
TypedArray attributes = theme.obtainStyledAttributes(typedValue.resourceId, R.styleable.MaterialAccount);
try {
username.setTextColor(attributes.getColor(R.styleable.MaterialAccount_titleColor, 0xFFF));
usermail.setTextColor(attributes.getColor(R.styleable.MaterialAccount_subtitleColor, 0xFFF));
} finally {
attributes.recycle();
}
// set the button image
if (!singleAccount) {
userButtonSwitcher.setImageResource(R.drawable.ic_arrow_drop_down_white_24dp);
userButtonSwitcher.setOnClickListener(accountSwitcherListener);
}
} else
customDrawerHeader = (LinearLayout) this.findViewById(R.id.drawer_header);
sections = (LinearLayout) this.findViewById(R.id.sections);
bottomSections = (LinearLayout) this.findViewById(R.id.bottom_sections);
// init lists
sectionList = new LinkedList<>();
bottomSectionList = new LinkedList<>();
accountManager = new LinkedList<>();
accountSectionList = new LinkedList<>();
subheaderList = new LinkedList<>();
elementsList = new LinkedList<>();
childFragmentStack = new LinkedList<>();
childTitleStack = new LinkedList<>();
// init listeners
if (drawerHeaderType == DRAWERHEADER_ACCOUNTS) {
userphoto.setOnClickListener(currentAccountListener);
if (singleAccount)
usercover.setOnClickListener(currentAccountListener);
else
usercover.setOnClickListener(accountSwitcherListener);
userSecondPhoto.setOnClickListener(secondAccountListener);
userThirdPhoto.setOnClickListener(thirdAccountListener);
}
// set drawer backgrond color
drawer.setBackgroundColor(drawerColor);
//get resources and density
resources = this.getResources();
density = resources.getDisplayMetrics().density;
// set the right drawer width
DrawerLayout.LayoutParams drawerParams = (android.support.v4.widget.DrawerLayout.LayoutParams) drawer.getLayoutParams();
drawerParams.width = Utils.getDrawerWidth(resources);
drawer.setLayoutParams(drawerParams);
// get primary color
theme.resolveAttribute(R.attr.colorPrimary, typedValue, true);
primaryColor = typedValue.data;
theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
primaryDarkColor = typedValue.data;
// if device is kitkat
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
TypedArray windowTraslucentAttribute = theme.obtainStyledAttributes(new int[] { android.R.attr.windowTranslucentStatus });
kitkatTraslucentStatusbar = windowTraslucentAttribute.getBoolean(0, false);
if (kitkatTraslucentStatusbar) {
Window window = this.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
RelativeLayout.LayoutParams statusParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, resources.getDimensionPixelSize(R.dimen.traslucentStatusMargin));
statusBar.setLayoutParams(statusParams);
statusBar.setImageDrawable(new ColorDrawable(darkenColor(primaryColor)));
if (drawerHeaderType == DRAWERHEADER_ACCOUNTS) {
RelativeLayout.LayoutParams photoParams = (RelativeLayout.LayoutParams) userphoto.getLayoutParams();
photoParams.setMargins((int) (16 * density), resources.getDimensionPixelSize(R.dimen.traslucentPhotoMarginTop), 0, 0);
userphoto.setLayoutParams(photoParams);
}
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
// INIT TOOLBAR ELEVATION
if (toolbarElevation) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// 4 dp elevation
toolbar.setElevation(4 * density);
} else {
View elevation = LayoutInflater.from(this).inflate(R.layout.layout_toolbar_elevation, content, false);
content.addView(elevation);
}
}
// INIT ACTION BAR
this.setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
// DEVELOPER CALL TO INIT
init(savedInstanceState);
if (sectionList.size() == 0) {
throw new RuntimeException("You must add at least one Section to top list.");
}
if (deviceSupportMultiPane()) {
// se il multipane e' attivo, si e' in landscape e si e' un tablet allora si passa in multipane mode
layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN, drawer);
DrawerLayout.LayoutParams params = new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.setMargins((int) (320 * density), 0, 0, 0);
content.setLayoutParams(params);
layout.setScrimColor(Color.TRANSPARENT);
layout.openDrawer(drawer);
layout.setMultipaneSupport(true);
} else {
// se non si sta lavorando in multiPane allora si inserisce il pulsante per aprire/chiudere
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
pulsante = new MaterialActionBarDrawerToggle<Fragment>(this, layout, toolbar, R.string.nothing, R.string.nothing) {
@Override
public void onDrawerClosed(View view) {
// creates call to onPrepareOptionsMenu()
invalidateOptionsMenu();
// abilita il touch del drawer
setDrawerTouchable(true);
if (drawerListener != null)
drawerListener.onDrawerClosed(view);
if (hasRequest()) {
MaterialSection section = getRequestedSection();
changeToolbarColor(section);
setFragment((Fragment) section.getTargetFragment(), section.getTitle(), (Fragment) currentSection.getTargetFragment());
afterFragmentSetted((Fragment) section.getTargetFragment(), section.getTitle());
this.removeRequest();
}
}
@Override
public void onDrawerOpened(View drawerView) {
// creates call to onPrepareOptionsMenu()
invalidateOptionsMenu();
if (drawerListener != null)
drawerListener.onDrawerOpened(drawerView);
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// if user wants the sliding arrow it compare
if (slidingDrawerEffect)
super.onDrawerSlide(drawerView, slideOffset);
else
super.onDrawerSlide(drawerView, 0);
if (drawerListener != null)
drawerListener.onDrawerSlide(drawerView, slideOffset);
}
@Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
if (drawerListener != null)
drawerListener.onDrawerStateChanged(newState);
}
};
pulsante.setToolbarNavigationClickListener(toolbarToggleListener);
layout.setDrawerListener(pulsante);
layout.setMultipaneSupport(false);
}
// si procede con gli altri elementi dopo la creazione delle viste
ViewTreeObserver vto = drawer.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// quando l'immagine e' stata caricata
// change user space to 16:9
int width = drawer.getWidth();
int heightCover = 0;
switch(drawerHeaderType) {
default:
case DRAWERHEADER_ACCOUNTS:
case DRAWERHEADER_IMAGE:
case DRAWERHEADER_CUSTOM:
// si fa il rapporto in 16 : 9
heightCover = (9 * width) / 16;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
heightCover += (int) (24 * density);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT && kitkatTraslucentStatusbar)
heightCover += (int) (25 * density);
break;
case DRAWERHEADER_NO_HEADER:
break;
}
// set user space
if (drawerHeaderType == DRAWERHEADER_ACCOUNTS) {
usercover.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, heightCover));
usercoverSwitcher.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, heightCover));
} else {
customDrawerHeader.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, heightCover));
}
// adding status bar height for other version of android that not have traslucent status bar
if ((Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT && !kitkatTraslucentStatusbar) || Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
heightCover += (int) (25 * density);
}
// heightCover (DRAWER HEADER) + 8 (PADDING) + sections + 8 (PADDING) + 1 (DIVISOR) + bottomSections + subheaders
int heightDrawer = (int) (((8 + 8) * density) + 1 + heightCover + sections.getHeight() + ((density * 48) * bottomSectionList.size()) + (subheaderList.size() * (35 * density)));
// create the divisor
View divisor = new View(MaterialNavigationDrawer.this);
divisor.setBackgroundColor(Color.parseColor("#8f8f8f"));
// si aggiungono le bottom sections
if (heightDrawer >= Utils.getScreenHeight(MaterialNavigationDrawer.this)) {
// add the divisor to the section view
LinearLayout.LayoutParams paramDivisor = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
paramDivisor.setMargins(0, (int) (8 * density), 0, (int) (8 * density));
sections.addView(divisor, paramDivisor);
for (MaterialSection section : bottomSectionList) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (48 * density));
sections.addView(section.getView(), params);
}
} else {
// add the divisor to the bottom section listview
LinearLayout.LayoutParams paramDivisor = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
bottomSections.addView(divisor, paramDivisor);
for (MaterialSection section : bottomSectionList) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (48 * density));
bottomSections.addView(section.getView(), params);
}
}
ViewTreeObserver obs = drawer.getViewTreeObserver();
// si rimuove il listener
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
}
});
MaterialSection section;
if (savedInstanceState == null) {
// init account views
if (accountManager.size() > 0) {
currentAccount = accountManager.get(0);
notifyAccountDataChanged();
}
// init section
if (defaultSectionLoaded < 0 || defaultSectionLoaded >= sectionList.size()) {
throw new RuntimeException("You are trying to open at startup a section that does not exist");
}
section = sectionList.get(defaultSectionLoaded);
if (section.getTarget() != MaterialSection.TARGET_FRAGMENT)
throw new RuntimeException("The first section added must have a fragment as target");
} else {
ArrayList<Integer> accountNumbers = savedInstanceState.getIntegerArrayList(STATE_ACCOUNT);
// ripristina gli account | restore accounts
for (int i = 0; i < accountNumbers.size(); i++) {
MaterialAccount account = accountManager.get(i);
account.setAccountNumber(accountNumbers.get(i));
if (account.getAccountNumber() == MaterialAccount.FIRST_ACCOUNT) {
currentAccount = account;
}
}
notifyAccountDataChanged();
int sectionSelected = savedInstanceState.getInt(STATE_SECTION);
int sectionListType = savedInstanceState.getInt(STATE_LIST);
if (sectionListType == Element.TYPE_SECTION) {
section = sectionList.get(sectionSelected);
} else
section = bottomSectionList.get(sectionSelected);
if (section.getTarget() != MaterialSection.TARGET_FRAGMENT) {
section = sectionList.get(0);
}
}
title = section.getTitle();
currentSection = section;
section.select();
setFragment((Fragment) section.getTargetFragment(), section.getTitle(), null, savedInstanceState != null);
// change the toolbar color for the first section
changeToolbarColor(section);
// add the first section to the child stack
childFragmentStack.add((Fragment) section.getTargetFragment());
childTitleStack.add(section.getTitle());
// learning pattern
if (learningPattern) {
layout.openDrawer(drawer);
disableLearningPattern();
}
}
Aggregations