-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A better approach for handling attributes and views. #99
Conversation
…c, removed virual nodes and using real views instead, dispatching attribute setters by attribute name string
…k for textwitcher
…aries (support-core-ui), commented out recycler view hand-written dsl
Hi, Btw, great work 👍 |
@gmarques33 Anvil 0.5.0 is planned to be released this week, and good news is that the public API is backwards-compatible with Anvil 0.4.0. Although, internally lots of things have changed, and new added APIs such as custom attribute factories or view factories or |
@zserge Cool. I look forward to trying the new changes. We are testing it in a project and I believe that 0.5.0 changes will help a lot. |
I'm trying to use the bottomNavigationView introduced in this merge, but simply cannot get it to trigger my NavigationItemSelectedListener. Is this functionality not complete? The documentation on such components is currently non-existent. @zserge class HomeView(c: Context) : RenderableView(c) {
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {
println("HOME")
return@OnNavigationItemSelectedListener true
}
R.id.navigation_dashboard -> {
println("Dash")
return@OnNavigationItemSelectedListener true
}
R.id.navigation_notifications -> {
println("Notif")
return@OnNavigationItemSelectedListener true
}
}
false
}
override fun view() {
frameLayout {
size(BaseDSL.MATCH, BaseDSL.MATCH)
bottomNavigationView {
gravity(BaseDSL.BOTTOM)
size(BaseDSL.MATCH, BaseDSL.WRAP)
onNavigationItemSelected(mOnNavigationItemSelectedListener)
bottomNavigationMenuView {
bottomNavigationItemView {
title("Home")
id(R.id.navigation_home)
icon(ContextCompat.getDrawable(context, R.drawable.ic_home_black_24dp))
}
bottomNavigationItemView {
title("Dash")
id(R.id.navigation_dashboard)
icon(ContextCompat.getDrawable(context, R.drawable.ic_home_black_24dp))
}
bottomNavigationItemView {
title("Notif")
id(R.id.navigation_notifications)
icon(ContextCompat.getDrawable(context, R.drawable.ic_home_black_24dp))
}
}
}
}
}
} |
@jakst Yes, it's another gift from Android Support Library developers, similar to #81 and friends. Basically, they are violating the common convention of the view constructors. BottomNavigationView is a view group, but it injects child views automatically, as well as having other side effects such as binding together the menu presented and menu view. In other words, you can't create a BottomNavigationView in pure java without using XMLs. Here's where the child view is added: https://android.googlesource.com/platform/frameworks/support/+/master/design/src/android/support/design/widget/BottomNavigationView.java#171 But this particular case is not so bar. One can specify the Menu resource in code, so I've been using the following view method in my component and it worked well: public void view() {
frameLayout(() -> {
size(MATCH, MATCH);
bottomNavigationView(() -> {
init(() -> {
((BottomNavigationView) Anvil.currentView()).inflateMenu(R.menu.bottom_nav_example);
});
gravity(BOTTOM);
size(MATCH, WRAP);
onNavigationItemSelected(mOnNavigationItemSelectedListener);
});
});
} Here I'm creating a view in java, but my menu items are declared in menu XML, much like the documentation suggests. Not as good as pure Kotlin (or Java), but it seems to be the only possible way. An alternative would be to re-write BottomNavigationView as your own class that would be Java- (and thus, Anvil-) friendly. |
@jakst You might want to open a separate issue if you would prefer to have a separate method like UPD: Sorry, it looks like |
This is what is likely to become Anvil 0.5.0 soon 🎉
Major changes:
Anvil.Node
class, using views directly. This should resolve Anvil.currentView() is null inside render method #83 and maybe other issues related to missynchronization between virtual view nodes and real views. In fact, working with views directly happens at the same speed as dealing with virtual nodes (unlike DOM vs virtual DOM in the web).attr("foo", someValue)
. It won't be fast, but works fine for one-time constant values.gravity
in DSL andgravity
in support DSL). Now such name clashes just work as expected.Benchmarks show that is gives a slight performance gain (~5%) comparing to Anvil 0.5.0, but the main code part becomes much simpler and the flexibility is extremely high, so I'm merging this into master branch.