收录日期:2020/10/28 07:17:19 时间:2011-06-18 13:56:32 标签:android,gallery

I have a gallery with 7 images. How can I get the position when stop the scrolling of a Gallery?

For reference you can handle the setOnItemSelectedListenerof the Galley component to do this.

    // Gallery On Item Selected
    mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

        // On Item Selected
        public void onItemSelected(CustomAdapterView<?> parent, View view,
                int position, long id) {

            // Do some custom action here!

        }
        //>

        // On Nothing Selected
        public void onNothingSelected(CustomAdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
        //>
    });
    //-->

Are you using an Adapter with the Gallery? If so, then AdapterView has a getSelectedItemPosition() function that returns an int.

You can subclass the ImageAdapter class and add a function that returns a private int which is always updated to the index of the last 'clicked' image; thus giving you the position of the current image.