开发者

Android: Stop Gallery Scrolling Without Choosing Item

开发者 https://www.devze.com 2023-03-06 16:45 出处:网络
I am currently working with a gallery view, and during onFling it gets scrolling pretty fast.My problem is that I h开发者_如何学Goave setup the onClick of each item to take you to a new activity.But I

I am currently working with a gallery view, and during onFling it gets scrolling pretty fast. My problem is that I h开发者_如何学Goave setup the onClick of each item to take you to a new activity. But I would only like this to work if the gallery is not currently scrolling. If the gallery is scrolling, I want the first click to just stop the scrolling at the current position.

Can anyone share code that will let me either check the scrolling state of a gallery, or custom gallery code that will stop the scrolling action?

Thanks, Josh


i guess is a little late for this answer, but here's the response for this issue, i just fixed it and want to share it with anyone whom is having the same issue...

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;
}}

Now all you have to do is, use the gallery as a regular component, assign it the OnItemClickListener and you will get the postion of the item without scrolling...

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);
                }
            });
0

精彩评论

暂无评论...
验证码 换一张
取 消