anvil-ui/anvil

XML inflating doesn't use LayoutParams provided by parent view

helgoboss opened this issue · 1 comments

I wanted to migrate my spinner adapter to Anvil by using the built-in XML layout support_simple_spinner_dropdown_item. So I started the view with

xml(R.layout.support_simple_spinner_dropdown_item) {

The basics worked but it looked different than before the migration (when I used inflate directly). I found out that it is related to this line in Anvil:

return LayoutInflater.from(c).inflate(xmlId, null, false);

If the second parameter of inflate is not null but the parent view, it works as expected. The parameter doc reads:

 * @param root Optional view to be the parent of the generated hierarchy (if
 *        <em>attachToRoot</em> is true), or else simply an object that
 *        provides a set of LayoutParams values for root of the returned
 *        hierarchy (if <em>attachToRoot</em> is false.)

So I guess the problem is the missing LayoutParams. Is there any specific reason why null is passed instead of the parent view? Could we have a second version of the xml directive which passes the parent view?

Thank you!

@helgoboss It makes perfect sense, thanks! The reason null was passed there is rather historical (I have been creating views in a much more complicated manner and they didn't know about their parent views at that time). Now it's absolutely possible to pass parent viewgroup to the fromXml() method, which I did in the recent commit 110082e

Thanks!