【问题标题】:Custom HorizontalScrollView management - enable/disable scroll自定义 Horizo​​ntalScrollView 管理 - 启用/禁用滚动
【发布时间】: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


    【解决方案1】:

    我认为有两个问题。首先,扩展到 ScrollView 而不是 Horizo​​ntalScrollView。

    其次,给垂直滚动指定一个固定的宽度。

    <ScrollView
        android:id="@+id/staffList_scrollview"
        android:layout_width="950dp"
        android:layout_height="555dp"
        android:layout_marginTop="10dp">
    

    如果你改变它应该可以工作

    <ScrollView
        android:id="@+id/staffList_scrollview"
        android:layout_width="match_parent"
        android:layout_height="555dp"
        android:layout_marginTop="10dp">
    

    测试这些更改并告诉我它是否有效。问候。

    【讨论】:

    • match_parent 不适合我的需求,但实际上是的,我没有注意到这个错误扩展......我的错。谢谢你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多