Android gallery without scroll by click

By default, it seems that when you click on a gallery item, the gallery automatically scrolls to center the item that was clicked. How can I cancel this behavior? I donโ€™t want the gallery to scroll to the center when pressed, I want it to stay where it is.

+4
source share
6 answers

I think this is the right solution:

@Override public boolean onSingleTapUp(final MotionEvent e) { boolean handled = super.onSingleTapUp(e); onDown(e); return handled; } 
+8
source

I think this is what you are looking for.

First create a class that extends from the Gallery, and then override the onSingleTapUp method:

 @Override public boolean onSingleTapUp(final MotionEvent e) { final OnItemClickListener listener = getOnItemClickListener(); final View selectedView = getSelectedView(); final float tapX = e.getRawX(); final float tapY = e.getRawY(); if ((selectedView != null) && (listener != null) && (tapX >= selectedView.getLeft()) && (tapX <= selectedView.getRight()) && (tapY >= selectedView.getTop()) && (tapY <= selectedView.getBottom())) { final int selectedPosition = getSelectedItemPosition(); listener.onItemClick(this, selectedView, selectedPosition, selectedPosition); } return true; } 
+2
source

Iโ€™ve never used a gallery before (in fact, I had to watch youtube watching to see the visual effect first ;-)

So, I dug up the gallery in the source code, and it seems to me that they tied a selection that is quite difficult to position, so you have to redefine the class and make a heavy code hack, possibly even reflection, to achieve your goal. I canโ€™t even tell if you will succeed.

This is not a solution, but a hint of what you expect, if you want to understand it; -)

+1
source

There is a way in the gallery to override this.

  gallery.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show(); } }); 

}

0
source

I have not tried this ... but you can try the following:

In onItemClickListener.onItemClick() determine the current selected position using Gallery.getSelectedItemPosition() , and then set the position using Gallery.setSelection(int position) . I don't know if this will work or not, but you can do it.

There is also an OnItemSelectedListener that you might try to use.

0
source

Unfortunately, I donโ€™t have the opportunity to comment on the posts of others yet, but for those who found this question but had problems, please note that if you create your own gallery that overrides the onSingleTap method (as suggested by Ohgema), you need to override the constructor which accepts Context and AttributeSet .

0
source

All Articles