Android自定义库禁用滚动

我正在尝试创建自定义图库以禁用滚动。 我从这里得到以下内容: 如何禁用图库视图滚动

public class MyGallery extends Gallery{ public MyGallery(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){ if (isSelected()) return true; else return super.onFling(e1, e2, velocityX, velocityY); } } 

似乎没有工作。 我究竟做错了什么?

注意示例中的if (isSelected())子句,您可能希望省略它并无条件地返回true ,完全避免inheritance的实现。

覆盖onFling可防止onFling但不会影响常规滚动,手指向下。 要做到这一点,请尝试覆盖onScroll并立即从那里返回true。

如果这也不起作用,您还可以覆盖onTouchEvent并在那里过滤触摸事件。

我在Linearlayout中使用了CustomListview。 并使用下面的代码禁用滚动

 public void enableDisableView(View view, boolean enabled) { view.setEnabled(enabled); if ( view instanceof ViewGroup ) { ViewGroup group = (ViewGroup)view; for ( int idx = 0 ; idx < group.getChildCount() ; idx++ ) { enableDisableView(group.getChildAt(idx), enabled); } } }