38

I'm using a GridView for a game board. Recently some users have had problems with the board scrolling vertically (on Samsung Galaxy / Vibrant phones running 2.2) -- This bug does not occur on my Nexus One.

One user produced some screenshots of the problem.

How could I lock the GridView in place? Is there a way to disable scrolling?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
pjama
  • 3,014
  • 3
  • 26
  • 27

2 Answers2

97

Try to add or override setOnTouchListener for GridView, then in onTouch method you can use code like this to make gridView not scrollable

Java

gridView.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return event.getAction() == MotionEvent.ACTION_MOVE;
    }

});

Kotlin

gridView.setOnTouchListener { v, event ->
    event.action == MotionEvent.ACTION_MOVE
}
Michael
  • 9,639
  • 3
  • 64
  • 69
Fadhlan
  • 1,144
  • 14
  • 14
11

You can try setEnabled(false), although it might have other side effects. GridView is really not meant to be used the way you are using it. You should create your own custom view or layout. You could also use a TableLayout.

Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • Yep, I realize it should be a custom view. Will be a bit more involved to replace the current GridView (Early design error). Was hoping there would be a quick fix :) The setEnabled() method disables all functionality. Thanks Romain – pjama Jan 31 '11 at 20:36
  • 1
    Romain Guy reeeeally does not want you to use a GridView without scrolling. I use this http://stackoverflow.com/a/4536955/671543 trick. – njzk2 Dec 02 '11 at 14:22
  • For future readers I have found one example with TableLayout: http://stackoverflow.com/a/8639461/3844201 – DeniSHow Oct 31 '16 at 16:16