如何自动滚动 GWT SuggestBox
在 PopupPanel
上设置最大高度持SuggestBox
?目前,当用户按下键盘向上键和向下键时,建议项目的样式会发生变化,然后按 Enter 将选择列表中当前选定的项目。
当项目位于低于最大高度时,滚动条不会滚动。
我尝试扩展 SuggestBox
和内部类(Class)DefaultSuggestionDisplay
覆盖 moveSelectionDown()
和 moveSelectionUp()
显式调用 popup.setScrollTop()
.
为了做到这一点,我需要访问当前选择的 MenuItem
的绝对顶部。因此需要访问 SuggestionMenu
这也是 SuggestBox 的内部类,它是私有(private)的,并在 DefaultSuggestionDisplay
中声明为私有(private)成员没有 setter/getter 。由于 GWT 是一个 JavaScript,我们不能使用反射来访问它......有没有人有这个问题的解决方法?
谢谢。
最佳答案
我一直在四处寻找,找不到合适的解决方案(除了重新实现 SuggestBox)。以下避免了重新实现 SuggestBox:
private static class ScrollableDefaultSuggestionDisplay extends SuggestBox.DefaultSuggestionDisplay {
private Widget suggestionMenu;
@Override
protected Widget decorateSuggestionList(Widget suggestionList) {
suggestionMenu = suggestionList;
return suggestionList;
}
@Override
protected void moveSelectionDown() {
super.moveSelectionDown();
scrollSelectedItemIntoView();
}
@Override
protected void moveSelectionUp() {
super.moveSelectionUp();
scrollSelectedItemIntoView();
}
private void scrollSelectedItemIntoView() {
// DIV TABLE TBODY TR's
NodeList<Node> trList = suggestionMenu.getElement().getChild(1).getChild(0).getChildNodes();
for (int trIndex = 0; trIndex < trList.getLength(); ++trIndex) {
Element trElement = (Element)trList.getItem(trIndex);
if (((Element)trElement.getChild(0)).getClassName().contains("selected")) {
trElement.scrollIntoView();
break;
}
}
}
}
关于gwt - 如何使用最大高度和溢出-y : scroll? 自动滚动 GWT SuggestBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8206982/