7

I am doing a task which retrieves images from server and displays in GridView in the application. This Grid view is scrolling up and down. But i want to scroll this view left to right as the menu screen scrolls. Is it possible with grid view? Or Is there any better way to do this? please help me in doing this.

thanks in advance.

wolverine
  • 1,665
  • 5
  • 24
  • 43
  • See also http://stackoverflow.com/questions/5418775/horizontal-scrolling-in-android-gridview . I haven't found a way to make GridView scroll horizontally, seems one solution is to switch to a Gallery, but those only have one row. – Anton I. Sipos Feb 05 '13 at 08:59

3 Answers3

6

This isn't easily possible with the stock Android GridView. Try using this library: two-way-gridview

(I found this library in this other answer: Horizontal scrolling grid view)

Community
  • 1
  • 1
Anton I. Sipos
  • 3,493
  • 3
  • 27
  • 26
4

Is there any better way to do this?

Yes, there is.

You can achieve this in 3 lines using a RecyclerView with a horizontal GridLayoutManager:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rec1);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.HORIZONTAL, false));
recyclerView.setAdapter(new CustomAdapter(arrayList));

The RecyclerView supports applications built with the SDK 7 or larger.

If you want to make it even easier, take a look at the HorizontalGridView class if you are working with an application that is built for the API 17 or larger.


Here's a link of an example of a simple RecyclerView Adapter.

Evin1_
  • 12,292
  • 9
  • 45
  • 47
  • 1
    I used the HorizontalGridView when numbersOfRow = 1 is worked, but numbersOfRow >= 2 the vertical direction will be uncontrolled. I can't find any method to set edge distance of vertical orientation, `setVerticalMargin(int margin)` just can set the margin in pixels between two child items vertically. – Codios Aug 15 '16 at 08:00
-6

Try with below layout

 <HorizontalScrollView android="http://schemas.android.com/apk/res/android" 
        android:layout_width="wrap_content" android:layout_height="wrap_content">
        <GridView android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/gridview" android:columnWidth="50dp" android:padding="10dp" 
            android:horizontalSpacing="8dp" android:verticalSpacing="12dp" 
            android:numColumns="3" android:scrollbars="horizontal"/>
    </HorizontalScrollView>
Nermeen
  • 15,883
  • 5
  • 59
  • 72
Harish
  • 3,122
  • 2
  • 31
  • 46