简体   繁体   English

如何禁用ScrollView滚动?

[英]How to disable ScrollView scrolling?

Trying to resolve this issue : How to disable pullToRefreshScrollView from listening to touch I am wondering it there is a solution, to block ScrollView from handling onTouchEvents without creating customizable class for it ? 试图解决这个问题: 如何禁用pullToRefreshScrollView从听取触摸我想知道它有一个解决方案,阻止ScrollView处理onTouchEvents而不为它创建可自定义的类? Why all the methos like 为什么所有的方法都喜欢

gridView.getParent().requestDisallowInterceptTouchEvent(true);

mScrollView.setOnTouchListener(new OnTouchListener() {
           @Override
           public boolean onTouch(View v, MotionEvent event) {
              return false;
           }
        });

doesn't work ? 不起作用? what's the problem with them ? 他们有什么问题? why Google implements methods which doesn't work ? 谷歌为什么实施不起作用的方法?

// Get the ScrollView //获取ScrollView

final ScrollView myScroll = (ScrollView) findViewById(R.id.display_scrollview);

// Disable Scrolling by setting up an OnTouchListener to do nothing //通过设置OnTouchListener来执行任何操作来禁用滚动

myScroll.setOnTouchListener( new OnTouchListener(){ 
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true; 
    }
});

// Enable Scrolling by removing the OnTouchListner //通过删除OnTouchListner启用滚动

tvDisplayScroll.setOnTouchListener(null);    

Create a custom ScrollView and use it wherever you wants. 创建自定义ScrollView并在任何您想要的地方使用它。

class CustomScrollView extends ScrollView {

    // true if we can scroll the ScrollView
    // false if we cannot scroll 
    private boolean scrollable = true;

    public void setScrollingEnabled(boolean scrollable) {
        this.scrollable = scrollable;
    }

    public boolean isScrollable() {
        return scrollable;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // if we can scroll pass the event to the superclass
                if (scrollable) return super.onTouchEvent(ev);
                // only continue to handle the touch event if scrolling enabled
                return scrollable; // scrollable is always false at this point
            default:
                return super.onTouchEvent(ev);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Don't do anything with intercepted touch events if 
        // we are not scrollable
        if (!scrollable) return false;
        else return super.onInterceptTouchEvent(ev);
    }

}

This can be use in layout 这可以在布局中使用

<com.packagename.CustomScrollView 
    android:id="@+id/scrollView" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent">

</com.packagename.CustomScrollView >

Then invoke 然后调用

((CustomScrollView )findViewById(R.id.scrollView)).setIsScrollable(false);

Try and see: 试着看看:

        scrollView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return isBlockedScrollView;
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 启用viewSwitcher后,如何在ScrollView中禁用滚动? - How to disable scrolling in ScrollView when the viewSwitcher is enabled? Nativescript:如何以编程方式禁用/启用 ScrollView 滚动? - Nativescript: how to programmatically disable / enable ScrollView scrolling? 禁用通过手势滚动ScrollView - Disable scrolling a ScrollView by gesture Android ScrollView 禁用惯性滚动 - Android ScrollView disable Inertial scrolling 禁用列表视图在scrollview中滚动 - disable listview scrolling inside scrollview Android:如何在滚动Horizo​​ntalScrollView时禁用ScrollView的垂直滚动? - Android: How to disable vertical scroll of ScrollView when scrolling a HorizontalScrollView? 如何禁用回收器视图滚动以便它侦听 Scrollview 布局? - how to disable recycler view scrolling so that it listens to Scrollview layout? 如何禁用RecyclerView滚动使布局侦听其ScrollView父级? - How to disable RecyclerView scrolling to make the layout listen its ScrollView parent? 如果不需要,如何禁用 ScrollView 滚动? - How can I disable ScrollView scrolling if it's not needed? (Android)如何禁用ScrollView中的所有按钮/复选框/其他小组件,但不禁用滚动 - (Android) How to disable all Buttons/CheckBoxes/Other Widgets inside a ScrollView but not disable the scrolling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM