ScrollView嵌套ListView焦点移动ListView不滚动的问题
当焦点在GridView上移动时,焦点已经移动到屏幕外,但是ScrollView不会跟随滚动。
即使在ListView的keydown事件中调用scrollview的executeKeyEvent
方法仍然不行,
View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
上一方法根本找不到下一个焦点view。看来只能自己手动滚动了,scrollview的滚动可以调用scrollBy
,如果想平滑滚动那就调用smoothScrollBy
。
在onItemSelected
函数中进行滚动方向的判断,滚动距离的计算;需要知道ScrollView的最大高度,记录上一次滚动位置。代码如下:
myListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int top = view.getTop();
int bottom = view.getBottom();
if (position > lastPos) {
if (bottom > bottomLimit) {
int height = bottom - bottomLimit;
bottomLimit += height;
scrollView.scrollBy(0, height);
}
}
else if(position < lastPos){
if (bottomLimit - top > screenHeight) {
int height = bottomLimit - top - screenHeight;
bottomLimit -= height;
scrollView.scrollBy(0, -(height));
}
}
lastPos = position;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
bottomLimit初始值就是Scrollview的高度;
嵌套GridView类似;