10

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

gridView.getParent().requestDisallowInterceptTouchEvent(true);

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

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

4

3 回答 3

24

// 获取滚动视图

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

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

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

// 通过移除 OnTouchListner 启用滚动

tvDisplayScroll.setOnTouchListener(null);    
于 2013-09-06T08:05:43.483 回答
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);
    }

}

这可以在布局中使用

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

</com.packagename.CustomScrollView >

然后调用

((CustomScrollView )findViewById(R.id.scrollView)).setIsScrollable(false);
于 2013-09-06T08:07:04.063 回答
7

试试看:

        scrollView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return isBlockedScrollView;
        }
    });
于 2013-09-06T08:06:14.053 回答