如何禁止scrollView 的子控件自动滑到 底部或者中间部分

现象:当一个scrollView 里面包含很多childView,并且整个界面超出屏幕的范围,而且每个childView都获取焦点,scrollView就会自动滑到底部或者中间部分。

可以使用以下几种方法解决:

 :有点绕,基本思路,就是让scrollView优先于childView获取到焦点,
private void disableAutoScrollToBottom() {
mScrollView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
mScrollView.setFocusable(true);
mScrollView.setFocusableInTouchMode(true);
mScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.requestFocusFromTouch();
return false;
}
});
}
恢复默认状态,禁掉scrollview的focus,这样就允许childview自动滑动
private void enableChildAutoScrollToBottom() {
mScrollView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
mScrollView.setFocusable(false);
mScrollView.setFocusableInTouchMode(false);
mScrollView.setOnTouchListener(null);
}
方法二:
将可能自动滑动的childview的focus禁掉,防止它自动滑动
mContentTextBox.setFocusable(false);
恢复默认状态,允许childview的focus,使它可以自动滑动
mContentTextBox.setFocusableInTouchMode(true);
mContentTextBox.setFocusable(true);
这里要注意,仅仅setFocusable为true是不够的,需要设setFocusableInTouchMode。
方法三:重写ScrollView的方法
@Override
protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) { return ;
}
或者
@Override
protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
return true;
}
 
上一篇:css3波浪形loading动画


下一篇:在线HTTP POST/GET模拟请求api接口http请求测试工具https://post.jsonin.com/