0

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

Okn
  • 698
  • 10
  • 21

1 Answers1

0

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.

Aksiom
  • 1,565
  • 4
  • 25
  • 39
  • Yes, i know it work with RecyclerView, but can i have to create recyclerView for just 4 items ? – Okn Jan 11 '16 at 14:01
  • But can I use this RecylerView in other RecylerView ? Because the parent is also a RecyclerView – Okn Jan 11 '16 at 14:49
  • Yes you can do that. Check this example where they show Cards inside of a RecyclerView and every Card has a new RecyclerView. So it is basicly a RecyclerView of RecyclerViews. http://stackoverflow.com/questions/28166624/android-how-can-i-insert-a-recyclerview-inside-cardview – Aksiom Jan 11 '16 at 15:11
  • After many tries, i can display image in RecyclerView but i have a problem beacause the second RecyclerView must have a defined height. For StaggeredGridLayoutManager i have to set height to "wrap_content". There is a trick ? – Okn Jan 11 '16 at 15:59