Sections with custom defined onClick actions #39
Comments
mmm, for this I can extend a new listener for appling a listener instead using a fragment or a intent. |
I create another three methods for creating sections: public MaterialSection newSection(String title, Drawable icon, MaterialSectionListener target)
public MaterialSection newSection(String title, Bitmap icon,MaterialSectionListener target)
public MaterialSection newSection(String title,MaterialSectionListener target) Now you can create your custom methods. this is an example of implementation: section2 = this.newSection("Section 2",new MaterialSectionListener() {
@Override
public void onClick(MaterialSection section) {
Toast.makeText(MainActivity.this,"Section 2 Clicked",Toast.LENGTH_SHORT).show();
}
}); |
Here's a problem with it @neokree in MaterialNavigationDrawer.java in onCreate(): setFragment((Fragment) section.getTargetFragment(), section.getTitle(), null); There might be a section with a custom listener or Activity so I still have to use my modified lib as a project module, which solves this. if(section.getTargetType() == MaterialSection.TargetType.FRAGMENT) {
setFragment((Fragment)section.getTargetFragment(),section.getTitle());
} else if(section.getTargetType() == MaterialSection.TargetType.ACTIVITY) {
this.startActivity(section.getTargetIntent());
} else{
MaterialSectionListener customListener = section.getCustomOnClickListener();
if (customListener != null)
customListener.onClick(section);
} I've created an enum in Material Section which I set in each constructor depending on type of a section & also generalized a creation of sections: // create sections
private MaterialSection newSectionGeneric(String title, Object icon, Object target, SectionType sectionType) {
if(sectionType == null) sectionType = SectionType.TOP;
MaterialSection section = new MaterialSection<Fragment>(
this,
sectionType==SectionType.TOP? sectionList.size() : BOTTOM_SECTION_START + bottomSectionList.size(),
icon==null? false : true
);
section.setTitle(title);
section.setIcon(icon);
section.setTarget(target);
section.setOnClickListener(this);
return section;
}
public MaterialSection newSection(String title, Drawable icon, Fragment target, SectionType sectionType) {
return newSectionGeneric(title, icon, target, sectionType);
}
// more constructors... But do it anyway you want, just please allow different section than fragment as a first section (initiated in onCreate). |
Sorry but I don't get it how the navigation drawer persists with an activity started on the onCreate method. For listeners, there are some complications, because for now you can't call the setFragment from the extended activity. So for now the first sections must have a fragment as target, because it was called when the activity starts... |
There are no complications here, firstly what I suggested is not in my own onCreate, but in your's in MaterialNavigationDrawer.java - I've updated my comment to make it more clear. This way, the first section might be with custom listener or even activity, not just fragment. Yes I also made setFragment public, because it's a useful method many people might want to use - and I suggest you to do that. Especially, if you have a custom listener & what you want to do is some logic & then set appropriate fragment, it's really helpful. The second part is an example how I simplified constructors to one line, made one generic one & how I used section & target types enum for an inspiration. If you want to see the whole code, I might share it :) maybe I made also some more methods public for a convenience. Is everything clear now @neokree? cheers mate |
Admirably, nav drawer is just not only for setting fragments / activities.
There are TON of use cases when app needs to have some custom logic and based on it, decide what will be done. E.g. sometimes, I need to check if there is added user / items I want to show so I trigger usual fragment, or just show placeholder, dialog or sth. Also Google apps need that - they trigger dialogs, open websites in some cases, trigger activity for result or some need to react if user has a conenction or not etc... list goes on and oooon.
More flexibility is clearly needed for an action which happens on drawer item click (selected) to allow user define custom action.
SOLUTION:
Currently because of that, I use an older version of a lib as a project mobule lib :-/ with a custom section constructors, custom section listener (extending the same MaterialSectionListener you already have) and I set it like section.setCustomOnClickListener and have some more logic to handle that... If set, I trigger this listener at the end of onClick impl in MaterialNavigationDrawer class, of course, not setting a new frag / activity previously, but letting a developer to do what's needed.
@neokree I would like to contribute to solve this, as this is almost the BIGGEST limitation this lib now has & it would be great to solve it asap. My approach doesn't have to be the best, but it works and it's kinda easy. Do not hesitate to contact me directly to figure this out (and also to test your newest features / imporvements before releasing them) ;)
The text was updated successfully, but these errors were encountered: