Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the quickest way to add several views to a LinearLayout?

I have a LinearLayout view that already contains several elements. I want to add a lot more Views to it, programmatically. And because this is inside a ScrollView, everything will be scrolled.

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.

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.

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?

I've tried doing this with a ListView (and adapter and a wrapper) and it seems to be much faster. The problem is that I can't use a simple ListView; my layout is more complex than a simple list (the LinearLayout itself contains additional custom icons, and it has another parent with even more Views before it's wrapped by the ScrollView).

But is there a way to use an adapter for a LinearLayout? Would that be faster than trying to add the views myself?

Any help is appreciated. I'd love to make this faster.

Code follows.

Main Activity:

// The LinearLayout that will contain everything
lineList = (LinearLayout) findViewById(R.id.lineList);

// Add a lot of items for testing
for (int i = 0; i < 10; i++) {
    addListItem("Item number " + i);
}

protected void addListItem(String __title) {
    MyListItem li;
    li = new MyListItem(this);
    li.setTitle(__title);
    lineList.addView(li);       
}

MyListItem:

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);
    }
}

What I'm trying to accomplish is this. Consider this layout:

Basic layout

This layout is a FrameLayout (outer box) containing a ImageView (in gray), a TextView (inner rectangle, on top) and a LinearLayout (inner rectangle, on bottom). This LinearLayout rectangle is the one I'm dynamically populating with a few items.

After I populate it, I want the final result to be this (where every new rectangle is a new MyListItem instance):

Populated layout

That is, everything is scrollable (the background image, for example, is aligned on top). The LinearLayout isn't scrollable by itself (everything else follows) hence why a ListView, from what I know, wouldn't work very well in my case.

like image 326
zeh Avatar asked Dec 16 '11 21:12

zeh


People also ask

How do I add a view to LinearLayout?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

How many views can you use within a ScrollView?

Only one view can be included in a ScrollView .

How are views aligned inside LinearLayout?

If you create a horizontal layout, child views will be aligned horizontally, Child views are vertically aligned if you set it to vertical. You can also change this orientation dynamically at runtime of the program by using setOrientation() method. The default way is to be align horizontally.

What is the difference between Framelayout and LinearLayout?

Frame Layout: This is designed to block out an area on the screen to display a single item. Linear Layout: A layout that arranges its children in a single column or a single row. Relative Layout: This layout is a view group that displays child views in relative positions.


2 Answers

3 Options:

  1. Replace everything with a ListView, with the other parent and custom icons as a header view for the ListView. ListView is faster, because it only creates Views as it needs them.

  2. Programatically create the contents of my_list_item instead of inflating, might be quicker

  3. Use of ViewStubs may allow you to load views on-demand.

  4. Maybe it isn't loading the views but the data? in which case prepare the data in a background thread.

like image 84
FunkTheMonk Avatar answered Nov 16 '22 01:11

FunkTheMonk


A ListView is the way to go.

You say that your layout is too complex. But it is completely okay to inflate a complex layout as a child. For example a layout that has text and then an icon:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <TextView
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" />
    <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" />
</LinearLayout>

Could be inflated in your adapter as so:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LinearLayout root = null;
    ImageView editImageView;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        root = (LinearLayout)inflater.inflate(R.layout.item, null);
    } else {
        root = (LinearLayout)convertView;
    }
}

You can also be a little more clever in order to support a header. Just add a check if the index is the root and inflate a different view. Since the header is the only one that is different you will still take advantage of all the other rows being reusable. You can even pre-inflate and store the header and reuse it to completely get rid of inflation.

like image 34
Grimmace Avatar answered Nov 16 '22 02:11

Grimmace