【发布时间】:2015-08-05 17:08:15
【问题描述】:
我正在尝试同时进行垂直和水平滚动,但有时我不需要激活此水平滚动。
所以我想要一个自定义的水平视图组件,我可以在其中管理我想要的水平滚动。
所以我实现了这个自定义的水平滚动类:
public class CustomHorizontalScroll extends ScrollView {
private boolean enableScrolling = true;
public boolean isEnableScrolling() {
return enableScrolling;
}
public void setEnableScrolling(boolean enableScrolling) {
this.enableScrolling = enableScrolling;
}
public CustomHorizontalScroll(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomHorizontalScroll(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomHorizontalScroll(Context context) {
super(context);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isEnableScrolling()) {
return super.onInterceptTouchEvent(ev);
} else {
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (isEnableScrolling()) {
return super.onTouchEvent(ev);
} else {
return false;
}
}
}
在我的活动中,我可以像这样启用或禁用滚动:
horizontalScrollView = (CustomHorizontalScroll) findViewById(R.id.horizontal_scroll_view);
horizontalScrollView.setEnableScrolling(true); // try first to set as enable by default to see if it works
但此时滚动总是被阻止,即使我像上面这个例子中的'true'一样通过。
这是我的带有垂直和水平滚动的布局代码:
<!-- Vertical scroll -->
<ScrollView
android:id="@+id/staffList_scrollview"
android:layout_width="950dp"
android:layout_height="555dp"
android:layout_marginTop="10dp">
<!-- Horizontal scroll -->
<com.myPackage.CustomHorizontalScroll
android:id="@+id/horizontal_scroll_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
....
</RelativeLayout>
</com.myPackage.CustomHorizontalScroll>
</ScrollView>
这可能是我的问题吗?我希望能够禁用和启用此水平滚动。但是目前使用这个自定义的水平类,这个滚动总是禁用我可以把它启用。
【问题讨论】:
标签: android android-layout horizontalscrollview