3

I have a GridView in an Activity and I want to scroll the GridView to a specific position using the scrollTo() method. Specifically, scroll the GridView by scrollTo() and alert a Dialog in the OnItemClickListener(), scroll back by scrollTo() in PositiveButton click.

If I just click the items in the GridView, it works well, but if I click the items after scrolling the GridView by finger, the GridView will scroll to the specific position and then scroll back immediately. I don't know why or how to deal with it.

gv.setOnItemClickListener(new OnItemClickListener() 
    { 
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
        { 
            gv.scrollTo(0, 200);
            AlertDialog.Builder dilog =new AlertDialog.Builder(MainActivity.this);
            dilog.setPositiveButton("aa", new OnClickListener(){

                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    gv.scrollTo(0, 0);
                }

            });
            dilog.create().show();
        } 
    }); 
Adrian Heine
  • 4,051
  • 2
  • 30
  • 43
Eric Liu
  • 31
  • 3

1 Answers1

5

GridView has some known issues with scrollTo(). Depending on your situation, there are a few workarounds, such as calling smoothScrollToPosition() or setSelection().

Community
  • 1
  • 1
acj
  • 4,821
  • 4
  • 34
  • 49
  • Should probably snap grid to nearest position when user's scroll completes so that `ScrollToPosition()` doesn't "jump" on restore. – samus Aug 28 '17 at 19:13