如何禁用ScrollView滚动?

fil*_*ski 9 android scrollview

试图解决这个问题:如何禁用pullToRefreshScrollView从听取触摸我想知道它有一个解决方案,阻止ScrollView处理onTouchEvents而不为它创建可自定义的类?为什么所有的方法都喜欢

gridView.getParent().requestDisallowInterceptTouchEvent(true);

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

不起作用?他们有什么问题?谷歌为什么实施不起作用的方法?

DrM*_*ens 19

//获取ScrollView

final ScrollView myScroll = (ScrollView) findViewById(R.id.display_scrollview);
Run Code Online (Sandbox Code Playgroud)

//通过设置OnTouchListener来执行任何操作来禁用滚动

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

//通过删除OnTouchListner启用滚动

tvDisplayScroll.setOnTouchListener(null);    
Run Code Online (Sandbox Code Playgroud)

  • 我已粘贴此解决方案,它不想工作. (4认同)

Ash*_*man 8

创建自定义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);
    }

}
Run Code Online (Sandbox Code Playgroud)

这可以在布局中使用

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

</com.packagename.CustomScrollView >
Run Code Online (Sandbox Code Playgroud)

然后调用

((CustomScrollView )findViewById(R.id.scrollView)).setIsScrollable(false);
Run Code Online (Sandbox Code Playgroud)

  • this.scrollable =可滚动; (2认同)

Aow*_*aza 7

试着看看:

        scrollView.setOnTouchListener(new View.OnTouchListener() {

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