【发布时间】:2012-05-05 13:05:14
【问题描述】:
我有HorizontalScrollView 作为孩子的长视图,所以HorizontalScrollView 是可滚动的并且可以水平滚动它的孩子。 有没有可能阻止它?我不希望用户能够滚动视图。
【问题讨论】:
-
滚动视图用于滚动!如果您不希望用户滚动,请选择使用 scrollView 以外的其他方式。您可以改用动画。
标签: android
我有HorizontalScrollView 作为孩子的长视图,所以HorizontalScrollView 是可滚动的并且可以水平滚动它的孩子。 有没有可能阻止它?我不希望用户能够滚动视图。
【问题讨论】:
标签: android
我的建议是使用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;
}
}
然后在xml文件中
<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>
【讨论】: