Trupti Nasit
Trupti Nasit

Reputation: 177

How can I have horizontal scrollview with 4 columns & 2 rows based on dynamic data?

I need 2 rows and 4 columns. If there are 10 items then 8 items will be shown first then remaining 2 will be shown when the view is scrolled horizontally(2nd view will have only 2 items).The number of items are added dynamically based on api response. enter image description here

Upvotes: 0

Views: 365

Answers (1)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

/**
     * Creates a vertical GridLayoutManager
     *
     * @param context Current context, will be used to access resources.
     * @param spanCount The number of columns in the grid
     */
    public GridLayoutManager(Context context, int spanCount) {
        super(context);
        setSpanCount(spanCount);
    }

You can try with RecyclerView

    int numberOfColumns = 4;
    recyclerViewOBJ.setLayoutManager(new GridLayoutManager(this, numberOfColumns));

Or

<HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     >

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="4"
        android:rowCount="2"
        android:orientation="horizontal" >

         // You can set Text Element

    </GridLayout>

 </HorizontalScrollView>

Upvotes: 1

Related Questions