How to disable ScrollView scrolling?

2019-01-15 01:14发布

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 ? 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 ?

3条回答
疯言疯语
2楼-- · 2019-01-15 01:25

Try and see:

        scrollView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return isBlockedScrollView;
        }
    });
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-15 01:44

// Get the ScrollView

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

// Disable Scrolling by setting up an OnTouchListener to do nothing

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

// Enable Scrolling by removing the OnTouchListner

tvDisplayScroll.setOnTouchListener(null);    
查看更多
Emotional °昔
4楼-- · 2019-01-15 01:48

Create a custom ScrollView and use it wherever you wants.

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);
查看更多
登录 后发表回答