Search code examples
androidstaggered-gridview

Android - LinearLayout childs dispose like StaggeredGridView


I have 4 elements (images) in LinearLayout which i want to dispose like the StaggeredGridView. Can I do this with a LinearLayout ? There is other method ?

enter image description here

The images are added dynamically


Solution

  • Do you consider using a RecyclerView with an StaggeredGridLayoutManager? An example: here

    If you are going to have always maximum of 4 items then you can do something like this:

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="2">
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
    
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scaleType="fitXY"
                    android:src="" />
    
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scaleType="fitXY"
                    android:src="" />
    
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
    
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scaleType="fitXY"
                    android:src="" />
    
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:scaleType="fitXY"
                    android:src="" />
    
            </LinearLayout>
    
        </LinearLayout>
    

    But I do not recommend this. I would go with the RecyclerView, nothing wrong with using it only for 4 items.