Java 如何在android ScrollView 上禁用和启用滚动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18893198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable and enable the scrolling on android ScrollView?
提问by Prosanto
I am a android developer.I also want to use a ScrollView.This ScrollView need to some time disable scrolling and Some time enable scrolling .But i can no able to disable the scrolling .How to i implement it .Please help to me.I also try to use the some code such a s
我是 android 开发人员。我也想使用 ScrollView。这个 ScrollView 需要一些时间禁用滚动和一些时间启用滚动。但我无法禁用滚动。如何实现它。请帮助我。我还尝试使用一些代码,例如
fullparentscrolling.setHorizontalFadingEdgeEnabled(false);
fullparentscrolling.setVerticalFadingEdgeEnabled(false);
or
或者
fullparentscrolling.setEnabled(false);
But it does not work.
但它不起作用。
采纳答案by Biraj Zalavadia
Try this way
试试这个方法
Create Your CustomScrollview like this
像这样创建您的自定义滚动视图
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;
public class CustomScrollView extends ScrollView {
private boolean enableScrolling = true;
public boolean isEnableScrolling() {
return enableScrolling;
}
public void setEnableScrolling(boolean enableScrolling) {
this.enableScrolling = enableScrolling;
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context) {
super(context);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isEnableScrolling()) {
return super.onInterceptTouchEvent(ev);
} else {
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (isEnableScrolling()) {
return super.onTouchEvent(ev);
} else {
return false;
}
}
}
In your xml
在你的 xml
// "com.example.demo" replace with your packagename
// "com.example.demo" 替换为你的包名
<com.example.demo.CustomScrollView
android:id="@+id/myScroll"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.example.demo.CustomScrollView>
In your Activity
在您的活动中
CustomScrollView myScrollView = (CustomScrollView) findViewById(R.id.myScroll);
myScrollView.setEnableScrolling(false); // disable scrolling
myScrollView.setEnableScrolling(true); // enable scrolling
回答by silvia_aut
Try this, this will disable the scroll function.
试试这个,这将禁用滚动功能。
scrollview.setEnabled(false);
回答by Hamad
You cannot disable the scrolling of a ScrollView. You would need to extend to ScrollView and override the onTouchEvent method to return false when some condition is matched.
您不能禁用 ScrollView 的滚动。您需要扩展到 ScrollView 并覆盖 onTouchEvent 方法以在匹配某些条件时返回 false。
You could modify ScrollView as follows to disable scrolling
您可以按如下方式修改 ScrollView 以禁用滚动
class LockableScrollView extends ScrollView {
...
// true if we can scroll (not locked)
// false if we cannot scroll (locked)
private boolean mScrollable = true;
public void setScrollingEnabled(boolean enabled) {
mScrollable = enabled;
}
public boolean isScrollable() {
return mScrollable;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// if we can scroll pass the event to the superclass
if (mScrollable) return super.onTouchEvent(ev);
// only continue to handle the touch event if scrolling enabled
return mScrollable; // mScrollable 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 (!mScrollable) return false;
else return super.onInterceptTouchEvent(ev);
}
}
for furthe reference here is the complete solution on stack overflow too! Disable ScrollView Programmatically?
为了进一步参考,这里也是堆栈溢出的完整解决方案! 以编程方式禁用 ScrollView?
mark this as answer for others help..thank you
将此标记为其他帮助的答案..谢谢
回答by themobius
I set the on touch listener for the scroll view and in the onTouch method i returend true. That did the trick for me.
我为滚动视图设置了触摸监听器,并在 onTouch 方法中返回 true。这对我有用。
mScrollView.setOnTouchListener( new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event)
{
return true;
}
});
Best for all
最适合所有人