阻止图库滚动项目选择

时间:2011-12-08 07:35:50

标签: android scroll android-gallery

如何在选择项目时阻止Gallery滚动? 到目前为止OnItemClickListener对我来说是不成功的。

gal.setOnItemClickListener(new OnItemClickListener() 
{
    public void onItemClick(AdapterView parent, View v, int position, long id) 
{
         ((ImageGallery) parent).setScrollingEnabled(false);
}
});

我已经将Gallery

分组了
public class ImageGallery extends Gallery
{
  private boolean stuck = false;

  public ImageGallery(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  public ImageGallery(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public ImageGallery(Context context) {
    super(context);
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) 
  {
    return stuck || super.onTouchEvent(event);
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_DPAD_LEFT:
    case KeyEvent.KEYCODE_DPAD_RIGHT:
      return stuck || super.onKeyDown(keyCode, event);
    }
    return super.onKeyDown(keyCode, event);
  }

  public void setScrollingEnabled(boolean enabled) 
  {
    stuck = !enabled;
  }
}

1 个答案:

答案 0 :(得分:1)

扩展库并覆盖onSingleTapUp方法,并使用performItemClick()方法为自定义实现执行click事件。