【问题标题】:Android HorizontalScrollView disable scrollingAndroid Horizo​​ntalScrollView 禁用滚动
【发布时间】:2012-05-05 13:05:14
【问题描述】:

我有Horizo​​ntalScrollView 作为孩子的长视图,所以Horizo​​ntalScrollView 是可滚动的并且可以水平滚动它的孩子。 有没有可能阻止它?我不希望用户能够滚动视图。

【问题讨论】:

  • 滚动视图用于滚动!如果您不希望用户滚动,请选择使用 scrollView 以外的其他方式。您可以改用动画。

标签: android


【解决方案1】:

我的建议是使用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;
    }
}

【讨论】:

  • 这拯救了我的一天!实际上,我花了一个小时研究如何禁用 TabLayout 中的小滚动。但我完全忘记了它是一种滚动视图!这也适用于我的情况。
  • 它拯救了我的一天。谢谢!
【解决方案2】:

好的,我找到了实现它的方法。

只需要创建自己的 Horizo​​ntalScrollView 并重写 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    相关资源
    最近更新 更多