【发布时间】:2019-08-27 17:11:06
【问题描述】:
当 HorizontalScrollView 中存在 EditText 或其他可聚焦视图时,它会在投掷时获得焦点。
深入源码,你会明白为什么:
/**
* Fling the scroll view
*
* @param velocityX The initial velocity in the X direction. Positive
* numbers mean that the finger/cursor is moving down the screen,
* which means we want to scroll towards the left.
*/
public void fling(int velocityX) {
if (getChildCount() > 0) {
int width = getWidth() - mPaddingRight - mPaddingLeft;
int right = getChildAt(0).getWidth();
mScroller.fling(mScrollX, mScrollY, velocityX, 0, 0,
Math.max(0, right - width), 0, 0, width/2, 0);
final boolean movingRight = velocityX > 0;
View currentFocused = findFocus();
View newFocused = findFocusableViewInMyBounds(movingRight,
mScroller.getFinalX(), currentFocused);
if (newFocused == null) {
newFocused = this;
}
if (newFocused != currentFocused) {
newFocused.requestFocus(movingRight ? View.FOCUS_RIGHT : View.FOCUS_LEFT);
}
postInvalidateOnAnimation();
}
}
一些建议的解决方法涉及使用以下属性:
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
这适用于一些简单的情况,但如果您的 HorizontalScrollView 嵌套在另一个 ScrollView 中,它可能会导致奇怪的行为(例如,外部滚动视图将跳转到现在被聚焦的容器)。
同样根据尝试此解决方案的经验,它可能需要将其添加到 每个 包含可聚焦视图的父容器(例如 EditText)。如果你有任何复杂的焦点逻辑,这一切都会失控。
还有其他变通的解决方案吗?
【问题讨论】:
标签: android android-scrollview horizontalscrollview