问题
Look at this simple code
Tree testTree=new Tree();
TreeItem testTreeItem=new TreeItem("test");
TreeItem testTreeItem2=new TreeItem("test2");
more TreeItem....
testTree.addItem(testTreeItem1);
testTree.addItem(testTreeItem2);
........
Now, resize your browser so that you can see the vertical scroll bar on the right hand-side. Now click on a tree item at the very bottom ex testTreeItem10
, you will see the the vertical scroll bar jump up instead of staying in the current position.
This error also happens when using UiBinder
<g:Tree ui:field="myTree">
<g:TreeItem text="Item 1" />
<g:TreeItem text="Item 2" /> .....
</g:Tree>
Also, addItem
of Tree
got deprecated, so how to solve this problem?
回答1:
This hacking code may work, but i am not sure it is the right way or elegant way to fix?
Could this hacking code break in the future?
By the way, just modify the Tree a bit:
Tree testTree=new Tree(){
public void onBrowserEvent(Event event) {
if (DOM.eventGetType(event) == Event.ONCLICK) {
return;
}
if (DOM.eventGetType(event) == Event.ONMOUSEDOWN) {
//int s = scrollPanel.getVerticalScrollPosition();
int scrollLeftInt = Window.getScrollLeft();
int scrollTopInt = Window.getScrollTop();
DOM.setStyleAttribute(this.getElement(), "position",
"fixed");
super.onBrowserEvent(event);
DOM.setStyleAttribute(this.getElement(), "position",
"static");
//scrollPanel.setVerticalScrollPosition(s);
Window.scrollTo(scrollLeftInt,scrollTopInt);
return;
}
super.onBrowserEvent(event);
}
};
回答2:
I was solving this problem some time ago. This code did the trick for me.
private Tree gwtTree = new Tree(){
@Override
public void setFocus(boolean focus) {
//Do not do anything!
}
};
来源:https://stackoverflow.com/questions/19131318/when-click-on-an-item-in-a-tree-it-makes-the-vertical-scroll-bar-jump-up