Android:停止图库滚动而不选择项目

时间:2011-05-17 10:34:24

标签: android gallery

我目前正在使用图库视图,在onFling期间,它可以快速滚动。我的问题是我已经设置了每个项目的onClick来带你进入一个新的活动。但是如果画廊当前没有滚动,我只希望这样。如果图库正在滚动,我希望第一次点击只是停止在当前位置滚动。

任何人都可以共享代码,让我可以检查图库的滚动状态,还是自动停止滚动操作的图库代码?

谢谢, 约什

1 个答案:

答案 0 :(得分:2)

我觉得这个答案有点晚了,但是这是对这个问题的回应,我刚刚解决了这个问题,并希望与遇到同样问题的人分享......

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