如何使用最大高度和溢出自动滚动GWT SuggestBox-y:滚动?

如何使用最大高度和溢出自动滚动GWT SuggestBox-y:滚动?,gwt,widget,scroll,Gwt,Widget,Scroll,如何在按住SuggestBox的PopupPanel上设置最大高度的情况下自动滚动GWTSuggestBox?当前,当用户按键盘向上键和向下键时,建议项目的样式会发生变化,按enter键将选择列表中当前选定的项目 当项目位于低于最大高度的位置时,滚动条不会滚动。 我尝试扩展SuggestBox和内部类DefaultSuggestDisplay以覆盖moveSelectionDown()和moveSelectionUp()以显式调用popup.setScrollTop() 为此,我需要访问当前选定

如何在按住
SuggestBox
PopupPanel
上设置最大高度的情况下自动滚动GWT
SuggestBox
?当前,当用户按键盘向上键和向下键时,建议项目的样式会发生变化,按enter键将选择列表中当前选定的项目

当项目位于低于最大高度的位置时,滚动条不会滚动。 我尝试扩展
SuggestBox
和内部类
DefaultSuggestDisplay
以覆盖
moveSelectionDown()
moveSelectionUp()
以显式调用
popup.setScrollTop()

为此,我需要访问当前选定的
MenuItem
的绝对顶部,因此需要访问
SuggestionMenu
,它也是SuggestBox的一个内部类,是私有的,在
DefaultSuggestionDisplay
中声明为私有成员,不带getter。因为GWT是一个JavaScript,所以我们不能使用反射来访问它。。。。有人有解决这个问题的方法吗


谢谢。

好的,我终于找到了解决办法。我必须基于GWT SuggestBox实现创建自己的建议框。但在自定义实现中,请遵循以下步骤: -将ScrollPanel放置到PopupPanel,然后将菜单栏放置到ScrollPanel -在新的内部SuggestionDisplat实现的moveSelectionUp()和moveSelectionDown()中添加以下代码:

 panel.ensureVisible( menu.getSelectedItem( ) );
扩展SuggestBox无法实现这一点,因为我们无法访问选定的 MenuItem,除非将受保护的getSelectionItem()方法重写为公共方法

最后添加CSS:

max-height: 250px;

到您的显示实现中的popupPanel

我一直在四处搜索,找不到合适的解决方案(除了重新实现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;
        }
    }
}
私有静态类ScrollableDefaultSuggestionDisplay扩展SuggestBox.DefaultSuggestionDisplay{
私有小部件建议菜单;
@凌驾
受保护的小部件装饰建议列表(小部件建议列表){
suggestionMenu=suggestionList;
返回建议列表;
}
@凌驾
受保护的void moveSelectionDown(){
super.moveSelectionDown();
滚动选择EditEmintoView();
}
@凌驾
受保护的无效moveSelectionUp(){
super.moveSelectionUp();
滚动选择EditEmintoView();
}
私有无效scrollSelectedItemIntoView(){
//DIV TABLE t车身变速器
NodeList trList=suggestionMenu.getElement().getChild(1.getChild(0.getChildNodes();
对于(int-trIndex=0;trIndex
}

继Google groups之后,我实现了一个类似的解决方案,由于使用了JSNI,该解决方案更加简洁:

private class ScrollableDefaultSuggestionDisplay extends DefaultSuggestionDisplay {

    @Override
    protected void moveSelectionDown() {
        super.moveSelectionDown();
        scrollSelectedItemIntoView();
    }

    @Override
    protected void moveSelectionUp() {
        super.moveSelectionUp();
        scrollSelectedItemIntoView();
    }

    private void scrollSelectedItemIntoView() {
        getSelectedMenuItem().getElement().scrollIntoView();
    }

    private native MenuItem getSelectedMenuItem() /*-{
        var menu = this.@com.google.gwt.user.client.ui.SuggestBox.DefaultSuggestionDisplay::suggestionMenu;
        return menu.@com.google.gwt.user.client.ui.MenuBar::selectedItem;
    }-*/;
}

我不确定,我明白你的意思。也许你需要展示一些代码或者做一把小提琴来解释这个问题。我刚才也做了同样的事情。最后,你找到了实现这个目标的方法吗?我和你呆在同一个地方,我需要访问当前选定的菜单项才能将其滚动到视图中。谢谢。我做了,请看下面的答案,如果你有任何问题,请告诉我。这看起来很有趣。。。你说这“避免重新实现SuggestBox”。。。但是,您是否至少需要重新实现SuggestBox构造函数才能使用ScrollableDefaultSuggestionDisplay而不是原始的DefaultSuggestionDisplay进行构造?谢谢哦,我想我看到你一定在调用公共的3-arg SuggestionBox构造函数,它的第3个arg是SuggestionBox.SuggestionDisplay(你的ScrollableDefaultSuggestionDisplay就是其中的一种)。是的,这很有效。。。别出心裁的解决方案@Perdi Estaquel!这是我在网上找到的唯一一个不侵入SuggestBox的解决方案。我想,如果在他们自己回答之前及时得到答案,Mayumi会接受这个答案的……请参阅Perdi Estaquel的另一个答案,它不需要侵入SuggestBox本身,而是使用一个简单的扩展。