0

How can I achieve having a large GridView and able to scroll vertically and horizontally?

I am able to scroll vertically but not horizontally. The horizontal width is fixed to the parent width and can't expand larger than the parent width.

The following screenshot is the example I want to achieve.

Cinema Seat Reservation

Andrew Lam
  • 3,675
  • 4
  • 23
  • 34
  • Possible duplicate of [Android GridView with Both Horizontal and Vertical Scrolbars at the same time](http://stackoverflow.com/questions/16299633/android-gridview-with-both-horizontal-and-vertical-scrolbars-at-the-same-time) – OneCricketeer Jan 11 '16 at 16:41
  • the solution from the post is by using table layout, but i need to use gridview http://stackoverflow.com/questions/16299633/android-gridview-with-both-horizontal-and-vertical-scrolbars-at-the-same-time – Andrew Lam Jan 11 '16 at 16:56
  • Did you look at the other answer? Duplicates don't necessarily mean the exact solution is what you are looking for, it also means the information that is gathered there does not need to be copied here. https://github.com/jess-anders/two-way-gridview – OneCricketeer Jan 11 '16 at 17:00

2 Answers2

0

I am able to scroll vertically with a RecyclerView and to scroll horizontally at the same time with a ViewPager. Try to implement a viewPager to scroll horizontally.

JeanCarrot
  • 41
  • 3
  • i wanted to scroll to view the whole gridview horizontally and vertically, because my gridview will contains many columns, and the screen size cannot fit – Andrew Lam Jan 11 '16 at 16:54
  • Okay you should maybe check the following answer then. http://stackoverflow.com/a/6716638/5655569 – JeanCarrot Jan 11 '16 at 17:10
0

The simplest solution is

  1. Use a horizontal scroll view to contain the GridView
  2. Then dynamically re-size the GridView width based on the number of columns that you set for the GridView
  3. Lastly, multiply number of columns with the width of each columns

    GridView gridView = (GridView) findViewById(R.id.grid_view);
    int numberOfColumns = 3;
    int sizeOfWidthPerColumn = 20;
    gridView.setNumColumns(numberOfColumns);
    ViewGroup.LayoutParams layoutParams = gridView.getLayoutParams();
    layoutParams.width = convertDpToPixels(numberOfColumns * sizeOfWidthPerColumn, this);
    gridView.setLayoutParams(layoutParams);
    

Now I am able to scroll the GridView horizontally and vertically.

Andrew Lam
  • 3,675
  • 4
  • 23
  • 34