我有一个HorizontalScrollView,其中包含一个很长的视图作为其子视图,因此HorizontalScrollView是可滚动的,并且可以在水平方向上滚动其子视图。 是否有可能阻止这种滚动?我不希望用户能够滚动视图。
有没有办法禁用HorizontalScrollView的滚动?
我有一个HorizontalScrollView,其中包含一个很长的视图作为其子视图,因此HorizontalScrollView是可滚动的,并且可以在水平方向上滚动其子视图。 是否有可能阻止这种滚动?我不希望用户能够滚动视图。
有没有办法禁用HorizontalScrollView的滚动?
我的建议是使用一个 OnTouchListener,例如:
在 onCreate
方法中
HorziontalScrollView scrollView= (HorizontalScrollView)findViewById(R.id.scrollView);
scrollView.setOnTouchListener(new OnTouch());
并且具有一个类:
private class OnTouch implements OnTouchListener
{
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
}
好的,我找到了实现这个的方法。
只需要创建自己的HorizontalScrollView并重写onTouchEvent方法即可。
public class MyHSV extends HorizontalScrollView {
public MyHSV(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public MyHSV(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyHSV(Context context) {
super(context);
init(context);
}
void init(Context context) {
// remove the fading as the HSV looks better without it
setHorizontalFadingEdgeEnabled(false);
setVerticalFadingEdgeEnabled(false);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Do not allow touch events.
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// Do not allow touch events.
return false;
}
}
<pathToClass.MyHSV xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scrollbars="none"
android:id="@+id/myHSV>
</pathToClass.MyHSV>