Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the quickest way to add several views to a LinearLayout?
    text
    copied!<p>I have a <code>LinearLayout</code> view that already contains several elements. I want to add a lot more Views to it, programmatically. And because this is inside a <code>ScrollView</code>, everything will be scrolled.</p> <p>So what I do is go through my list, and add new instances of my custom View to it. That custom view inflates a XML layout and adds a few methods.</p> <p>This approach works well. The problem is that it's super slow, even without any crazy code... a list with 10 items takes around 500ms to instantiate. As an user experience standpoint, this is hard to swallow.</p> <p>My question is, is this the correct/best approach? Android seems to take a lot of time inflating the layout, even though "R.layout.my_list_item" is super simple. I wonder if there's a way to maybe to reuse "inflated" layouts for additional views, kinda caching the more complex parsing?</p> <p>I've tried doing this with a <code>ListView</code> (and adapter and a wrapper) and it seems to be much faster. The problem is that I can't use a simple <code>ListView</code>; my layout is more complex than a simple list (the <code>LinearLayout</code> itself contains additional custom icons, and it has another parent with even more Views before it's wrapped by the <code>ScrollView</code>).</p> <p>But is there a way to use an adapter for a LinearLayout? Would that be faster than trying to add the views myself?</p> <p>Any help is appreciated. I'd love to make this faster.</p> <p>Code follows.</p> <p><strong>Main Activity:</strong></p> <pre><code>// The LinearLayout that will contain everything lineList = (LinearLayout) findViewById(R.id.lineList); // Add a lot of items for testing for (int i = 0; i &lt; 10; i++) { addListItem("Item number " + i); } protected void addListItem(String __title) { MyListItem li; li = new MyListItem(this); li.setTitle(__title); lineList.addView(li); } </code></pre> <p><strong>MyListItem:</strong></p> <pre><code>public class MyListItem extends RelativeLayout { protected TextView textTitle; public MyListItem(Context __context) { super(__context); init(); } public MyListItem(Context __context, AttributeSet __attrs) { super(__context, __attrs); init(); } public MyListItem(Context __context, AttributeSet __attrs, int __attrsdefStyle) { super(__context, __attrs, __attrsdefStyle); init(); } protected void init() { // Inflate the XML layout LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.my_list_item, this); // Create references textTitle = (TextView)findViewById(R.id.textTitle); } public void setTitle(String __text) { textTitle.setText(__text); } } </code></pre> <p>What I'm trying to accomplish is this. Consider this layout:</p> <p><img src="https://i.stack.imgur.com/5eNcu.png" alt="Basic layout"></p> <p>This layout is a <code>FrameLayout</code> (outer box) containing a <code>ImageView</code> (in gray), a <code>TextView</code> (inner rectangle, on top) and a <code>LinearLayout</code> (inner rectangle, on bottom). This <code>LinearLayout</code> rectangle is the one I'm dynamically populating with a few items.</p> <p>After I populate it, I want the final result to be this (where every new rectangle is a new <code>MyListItem</code> instance):</p> <p><img src="https://i.stack.imgur.com/9IVz1.png" alt="Populated layout"></p> <p>That is, everything is scrollable (the background image, for example, is aligned on top). The <code>LinearLayout</code> isn't scrollable by itself (everything else follows) hence why a <code>ListView</code>, from what I know, wouldn't work very well in my case.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload