Android: stop scrolling gallery without selecting an item

I am currently working with gallery browsing, and during onFling it scrolls pretty fast. My problem is that I configured onClick of each item to jump to a new action. But I would like it to work if the gallery is not currently scrolling. If the gallery scrolls, I want the first click to just stop scrolling at the current position.

Can someone use a code that allows me to check the scroll status of the gallery or my own gallery code that will stop the scroll action?

Thanks Josh

+3
source share
1 answer

, , , -, ...

public class CustomGallery extends Gallery {
public CustomGallery(Context context) {
   super(context);
}

public CustomGallery(Context context, AttributeSet attrs) {
   super(context, attrs);
}
public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
   super(context, attrs, defStyle);
}
    //In case you might want the gallery to start from left(another known issue...)
public void alignToLeft(Activity activity) {
    super.onAttachedToWindow();
    DisplayMetrics display = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(display);
    // start the images from the left...
    int scrollTo = (int) (display.widthPixels - getPixelsFromDIP(display,POSTER_WIDTH_DIP) - 2 * getPixelsFromDIP(display, 5));

    MarginLayoutParams margins = (MarginLayoutParams)getLayoutParams();
    MarginLayoutParams.class.cast(this.getLayoutParams()).setMargins(-scrollTo + margins.leftMargin, margins.topMargin, margins.rightMargin, margins.bottomMargin);
}

private int getPixelsFromDIP(DisplayMetrics display, int dps){
    return (int)(dps * display.scaledDensity + 0.5F);
}

    //Override single tap up, to prevent propagation of the event, and force
    //the listener to push the event calculating position from X,Y coords
private OnItemClickListener listener;
@Override
public boolean onSingleTapUp(MotionEvent e) {
    if(listener != null){
        listener.onItemClick(null, this, pointToPosition((int) e.getX(), (int) e.getY()), 0x0345);
    }else{
        return super.onSingleTapUp(e);
    }
    return false;
}

    //Override the method to assign our custom item listener...
@Override
public void setOnItemClickListener(
        android.widget.AdapterView.OnItemClickListener listener) {
    this.listener = listener;
}}

, , , OnItemClickListener, ...

final CustomGallery customGallery = (CustomGallery)personDetailView.findViewById(R.id.customGallery);
            customGallery(this);
            customGallery(castCrewAdapter);
            customGallery(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {
                    android.util.Log.e("customGallery TAG", "Item clicked: " + position);
                }
            });
+2