anL
anL

Reputation: 1073

LinearLayout performance with layout_weight attribute

I was trying to design the following view in my photo album app, and I finally did something after I ask that to the StackOverflow.

As I read about layout_weight attribute, when it is used in a nested structure, it may couse some performance problems.

I'm planning to use the following layout for repeated rows by loading while the user slides the screen down.

Here is the layout :

enter image description here

Here is the code :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button9"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#1000b0"
            android:textSize="15sp" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#d0b0b0"
                android:textSize="15sp" >

                <Button
                    android:id="@+id/button3"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#b170b0"
                    android:textSize="15sp" />

                <Button
                    android:id="@+id/button4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#b110b0"
                    android:textSize="15sp" />
            </LinearLayout>

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#1070b0"
                android:textSize="15sp" />
        </LinearLayout>
    </LinearLayout>

    <!-- The second line  --> 

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button11"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#a070b0"
            android:textSize="15sp" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="15sp" >

                <Button
                    android:id="@+id/button12"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#111"
                    android:textSize="15sp" />

                <Button
                    android:id="@+id/button13"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#b110b0"
                    android:textSize="15sp" />
            </LinearLayout>

            <Button
                android:id="@+id/button14"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#7510b0"
                android:textSize="15sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

Please note that I put the "button" just for test, they will be ImageView. I only used LinearLayout. Does it the right way to have this view (according to performance) ?

I wrote just 2 rows for now, it is gonna have more rows that are created dynamically on run time later. Does it get slow when the row count reaches, maybe 20 ?

Should I change the apporach to have this view ?

Thanks

Upvotes: 1

Views: 370

Answers (1)

Hariharan
Hariharan

Reputation: 24853

Try this..

Yes do all ListView see that Lazyadapter from that you can avoid it.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button9"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#1000b0"
            android:textSize="15sp" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#d0b0b0"
                android:textSize="15sp" >

                <Button
                    android:id="@+id/button3"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#b170b0"
                    android:textSize="15sp" />

                <Button
                    android:id="@+id/button4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#b110b0"
                    android:textSize="15sp" />
            </LinearLayout>

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#1070b0"
                android:textSize="15sp" />
        </LinearLayout>
    </LinearLayout>

    <!-- The second line -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button11"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#a070b0"
            android:textSize="15sp" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textSize="15sp" >

                <Button
                    android:id="@+id/button12"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#111"
                    android:textSize="15sp" />

                <Button
                    android:id="@+id/button13"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#b110b0"
                    android:textSize="15sp" />
            </LinearLayout>

            <Button
                android:id="@+id/button14"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#7510b0"
                android:textSize="15sp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button9"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#1000b0"
            android:textSize="15sp" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#d0b0b0"
                android:textSize="15sp" >

                <Button
                    android:id="@+id/button3"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#b170b0"
                    android:textSize="15sp" />

                <Button
                    android:id="@+id/button4"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#b110b0"
                    android:textSize="15sp" />
            </LinearLayout>

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#1070b0"
                android:textSize="15sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

Upvotes: 1

Related Questions