Comments (5)
Hi, @corbt! Glad to hear your interest to Anvil is back :)
As you might remember the old Anvil's config()
was needed only to get access to the real view object.
In the new Anvil you always have access to the real view object inside your renderable lambdas. Just call Anvil.currentView()
and you will get the view instance which is currently being modified, e.g.:
linearLayout {
val layout = (LinearLayout) Anvil.currentView();
// Here you may use real LinearLayout instance
button {
if (!myButtonIsInitialized) {
// Get real Button instance
val b = (Button) Anvil.currentView();
initializeMyButton(b);
myButtonIsInitialized = true;
}
text("Click me")
onClick { v -> ... }
....
}
}
The only catch here is that the first rendering cycle may happen to early and if you want, say, to get view width in pixels - it will return zero. So you may force rendering to be done in a loop again and again until view width becomes a valid number:
int mWidth = -1;
...
frameLayout(() -> {
if (mWidth <= 0) {
if ((mWidth = Anvil.currentView().getWidth()) == 0) {
post(Anvil::render);
}
}
});
from anvil.
Ok, sounds good. A related question: often I want to call a function on the view only on the first render (which was a property of the config
attribute). I suppose I could use a sentinel boolean to mark when my code has been run once and skip it after that, but is there a way to get this behavior built into the framework? If not I guess I'll just create my own config
that implements that behavior. :)
from anvil.
Sorry, at the moment sentinel boolean is the only solution. I'm still not sure if a function like config
is really needed in Anvil core. I expect most of the UI to be controlled in a declarative manner via Anvil DSL, so config
is rather an exception.
I would implement config as config(cb: () -> Boolean)
that would execute cb and re-schedule Anvil.render until the cb returns true. In simple cases it would run it once, in more complex it will stop once view is initialized. But such implementation would require to keep a flag variable bound to the view, which implies using a WeakHashMap etc. This generic solution can quickly become complicated for a small library like Anvil.
So I suggest writing config
-like wrappers manually for now, unless we see that every Anvil user does it and their implementations look very similar. Then it would be wise to have a generic implementation added to the Anvil core.
from anvil.
Ok. I suspect that this will be a useful/commonly-requested function, but if you want to leave it out until more people request it that's fine with me. I've included my implementation below so if anyone else needs this functionality they can copy it into their project.
public interface ConfigListener {
abstract public void config(View v);
}
public static void config(final ConfigListener listener) {
attr(configFunc.instance, listener);
}
private final static class configFunc implements Anvil.AttrFunc<ConfigListener> {
private final static configFunc instance = new configFunc();
@Override
public void apply(View view, ConfigListener listener, ConfigListener old) {
if(old == null)
listener.config(view);
}
}
from anvil.
Ok, Anvil 0.2.0 is here, it includes the 'init' function that runs the attached lambda only once, like:
textView {
init {
val tv: TextView = Anvil.currentView()
tv.setText("hello");
}
}
It follows the code you've submitted, just renamed it to 'init' since it seems to be more precise (initialization happens once for sure, while configuration may happen many times).
from anvil.
Related Issues (20)
- RecyclerView not found in XML layout
- Anvil fails silently when you set a property on a view that won't accept it HOT 1
- Anvil render question
- Anvil + Yoga HOT 2
- How to use anvil with ConstraintLayout? HOT 2
- Crash on setting text via DSL.text(CharSequence)
- Xml views are recreated on every view() call. HOT 1
- NoSuchElementException in Anvil.render() HOT 3
- Approaching React Native HOT 2
- Bug on swipeRefreshLayout
- Anvil.render() and ripple effect drawable problem HOT 1
- How to work with TypedArray parameters with Anvil? HOT 1
- I can't run in android studio 3.0 - module not specified HOT 10
- RenderableRecyclerViewAdapter is really recycling views?!
- BottomNavigationBar not visible
- Material components
- Still maintained HOT 5
- Transfer repo to org HOT 9
- Ask a question HOT 1
- Would be possible to run the diff tree algorithm in a non UI thread? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from anvil.