[英]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.