9

in my application , i have a gallery of pictures,

but i want to detect the position of the current image displayed ,

For example : when i launch my acvitivity, the position is 0, but when i scroll in my gallery,i want to get The position of the Current image displayed ,

i have tried OnFocusChanged , OnItemClicked , but that works only if i click on an item of my gallery

Any ideas ! :(

Houcine
  • 24,001
  • 13
  • 56
  • 83

3 Answers3

22
gal.setOnItemSelectedListener(new OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
            //Do something with position
            }
};

You get this callback every time a new item in the gallery gets put in the center, So be careful not to do anything to work intensive because if the user scrolls fast you are going to get a callback for each item they pass.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • thanks Tim again , it works for me , i want to get this position, for example , if i'm in the i eme position , i will download just the images i-1 , i , and i+1 , :) thanks a lot for your help , i appreciate that :) ciao – Houcine May 13 '11 at 15:48
  • You are right Tim. It is possible to put a little timer so to ignore selections if gallery is scrolled too fast, unless there is some inbuilt solution? – Lumis May 13 '11 at 15:51
  • There is no built in way to do this that I am aware of, but it should be posible to do it with a Handler and postDelayed() on a runnable that checks a flag to see if you are still on the same position. If the work that you are trying to do is not too intensive though the multiple callbacks won't be a problem – FoamyGuy May 13 '11 at 15:54
  • no , the multiple callbakcs won't be a problem, i wan't just if i'm in the position x , i should download the position x-1 and x+1 , and not x+2 or x-2 , but if i passed a position , it should be downloaded , :) – Houcine May 13 '11 at 15:58
  • any one have idea how it work in case of scroll view focus change? – CoDe Jan 30 '13 at 09:57
9

To eliminate the events during the fling, add this in addition to the setOnItemSelectedListener() call:

gal.setCallbackDuringFling(false);
jsmith
  • 4,847
  • 2
  • 32
  • 39
7

Use Gallery.getSelectedItemPosition()

OcuS
  • 5,320
  • 3
  • 36
  • 45