Search code examples
androidandroid-gallery

How to disable auto selection from gallery view


I ma using a Gallery view with images. I have the following onItemSelected -

public void onItemSelected(
        final AdapterView<?> parent,
        final View view,
        final int index,
        final long id) {



    GalleryAdapter.selected = index ;

      Animation growAnimation = AnimationUtils.loadAnimation(this,
              R.anim.grow_shrink_image);
      view.startAnimation(growAnimation);

     ...
}

It works fine, when an item is tapped it moves to the center and animation runs. But problem is, when it is scrolled left or right, the item which reached center gets automatically selected and animation starts. How can this auto selection be disabled?


Solution

  • I had same problem. So i have used setCallbackDuringFling().

    here g is an Object Of GalleryView.

    g.setCallbackDuringFling(false);
    
    g.setOnItemSelectedListener(new OnItemSelectedListener() {
    
                @Override
                public void onItemSelected(AdapterView<?> adapter, View view, int pos,
                        long arg3) {
                    // TODO Auto-generated method stub
                    Toast.makeText(MyActivity.this,"selected", 1000).show();
                                // here u can apply animation to selected image
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> adapter) {
                    // TODO Auto-generated method stub
    
                }
            });