Android - 按住按钮时禁止滚动
Android - Disable scrolling while holding button
我在 ScrollView
里面有一个 Button
。
<ScrollView>
<LinearLayout>
...
<Button/>
...
<LinearLayout/>
</ScrollView>
我正在使用此按钮重新编码语音。当它捕捉到 MotionEvent.ACTION_DOWN
事件时,我开始记录,在 MotionEvent.ACTION_UP
事件上我停止它。
buttomRecord.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
startRecording();
return true;
}
else if(event.getAction() == MotionEvent.ACTION_UP){
stopRecording();
return true;
}
return false;
}
});
问题是当用户正在录音时,如果这个 ScrollView
向下或向上滚动,这个按钮将失去焦点并且永远不会捕捉到 MotionEvent.ACTION_UP
事件。
如何在按住 Button
或禁用 [=14=] 滚动时将焦点锁定在 Button
上?
您可以使用
mScrollView.requestDisallowInterceptTouchEvent(true);
禁止 scrollview
的触摸事件
所以你的代码将是
buttomRecord.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
startRecording();
mScrollView.requestDisallowInterceptTouchEvent(true);
return true;
}
else if(event.getAction() == MotionEvent.ACTION_UP){
stopRecording();
mScrollView.requestDisallowInterceptTouchEvent(false);
return true;
}
return false;
}
});
使用这个自定义滚动视图:
public class CustomScrollView extends ScrollView {
private boolean mScrollable = true;
public void setScrollingEnabled(boolean enabled) {
mScrollable = enabled;
}
public boolean isScrollable() {
return mScrollable;
}
public CustomScrollView(Context context) {
super(context);
}
@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);
}}
然后在你的 xml:
<your.package.name.CustomScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
Android: scrollbars="vertical">
<!--whatever widgets you want to add-->
</your.package.name.CustomScrollView>
在你的activity中:
buttomRecord.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
scrollView.setScrollingEnabled(false);
startRecording();
return true;
}
else if(event.getAction() == MotionEvent.ACTION_UP){
stopRecording();
return true;
}
return false;
}
});
我在 ScrollView
里面有一个 Button
。
<ScrollView>
<LinearLayout>
...
<Button/>
...
<LinearLayout/>
</ScrollView>
我正在使用此按钮重新编码语音。当它捕捉到 MotionEvent.ACTION_DOWN
事件时,我开始记录,在 MotionEvent.ACTION_UP
事件上我停止它。
buttomRecord.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
startRecording();
return true;
}
else if(event.getAction() == MotionEvent.ACTION_UP){
stopRecording();
return true;
}
return false;
}
});
问题是当用户正在录音时,如果这个 ScrollView
向下或向上滚动,这个按钮将失去焦点并且永远不会捕捉到 MotionEvent.ACTION_UP
事件。
如何在按住 Button
或禁用 [=14=] 滚动时将焦点锁定在 Button
上?
您可以使用
mScrollView.requestDisallowInterceptTouchEvent(true);
禁止 scrollview
的触摸事件
所以你的代码将是
buttomRecord.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
startRecording();
mScrollView.requestDisallowInterceptTouchEvent(true);
return true;
}
else if(event.getAction() == MotionEvent.ACTION_UP){
stopRecording();
mScrollView.requestDisallowInterceptTouchEvent(false);
return true;
}
return false;
}
});
使用这个自定义滚动视图:
public class CustomScrollView extends ScrollView {
private boolean mScrollable = true;
public void setScrollingEnabled(boolean enabled) {
mScrollable = enabled;
}
public boolean isScrollable() {
return mScrollable;
}
public CustomScrollView(Context context) {
super(context);
}
@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);
}}
然后在你的 xml:
<your.package.name.CustomScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
Android: scrollbars="vertical">
<!--whatever widgets you want to add-->
</your.package.name.CustomScrollView>
在你的activity中:
buttomRecord.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
scrollView.setScrollingEnabled(false);
startRecording();
return true;
}
else if(event.getAction() == MotionEvent.ACTION_UP){
stopRecording();
return true;
}
return false;
}
});