问题描述
我正在开发一个Eclipse RCP应用程序,并尝试使用 ILazyTreeContentProvider
实现,以便在特定时间仅显示可见项目。
I am developing an Eclipse RCP application, and am trying to use a ILazyTreeContentProvider
implementation in order to show only the visible items at a certain time.
代码:
在扩展ViewPart的类中:
Inside the class extending ViewPart:
public void createPartControl(Composite parent) {
viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.VIRTUAL);
drillDownAdapter = new DrillDownAdapter(viewer);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
//viewer.setSorter(new NameSorter());
viewer.setInput(getViewSite());
// Create the help context id for the viewer's control
PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "test.provider.lazy.viewer");
makeActions();
hookContextMenu();
hookDoubleClickAction();
contributeToActionBars();
}
ContentProvider内部:
Inside the ContentProvider:
@Override
public void updateElement(Object parent, int index) {
System.out.println(updateElementCounter++);
if (parent.equals(getViewSite())) {
if (index == 0) {
TreeParent child = new TreeParent("Parent 1");
viewer.replace(parent, index, child);
viewer.setHasChildren(child, true);
} else {
TreeParent child = new TreeParent("Parent 2");
viewer.replace(parent, index, child);
viewer.setHasChildren(child, true);
//viewer.setChildCount(child, 1);
}
} else {
if (parent instanceof TreeParent) {
TreeParent treeParent = (TreeParent) parent;
if (treeParent.getName().equals("Parent 1")) {
TreeObject child = new TreeObject("Leaf "+index);
viewer.replace(treeParent, index, child);
viewer.setHasChildren(child, false);
//viewer.setChildCount(child, 0);
} else {
TreeObject child = new TreeObject("Special One");
viewer.replace(treeParent, index, child);
viewer.setHasChildren(child, false);
//viewer.setChildCount(child,0);
}
}
}
}
@Override
public void updateChildCount(Object element, int currentChildCount) {
if (element.equals(getViewSite())) {
viewer.setChildCount(getViewSite(), 2);
} else
if (element instanceof TreeParent) {
TreeParent parent = (TreeParent) element;
if (parent.getName().equals("Root")) {
viewer.setChildCount(parent, 2);
} else if (parent.getName().equals("Parent 1")) {
viewer.setChildCount(parent, 20);
} else {
viewer.setChildCount(parent, 1);
}
} else {
viewer.setChildCount(element, 0);
}
}
问题是每次我展开TreeItem的子项,它加载所有的子元素,而不仅仅是可见的子元素,而 System.out.println(updateElementCounter ++);
命令打印出所有22,尽管有只有7棵树项目可见。
The problem is that each time I expand the children of a TreeItem, it loads all the subelements, not just the visible ones, whereas the System.out.println(updateElementCounter++);
command prints out all 22 despite having only 7 Tree Items visible.
不应该 ILazyTreeContentProvider
只加载可见的? (在这种情况下它是7个项目)
Shouldn't the ILazyTreeContentProvider
only load the ones that are visible? (In this case it's 7 items)
我必须做错事...
无论如何,任何意见非常感谢!
Anyways, any opinions are very appreciated!
推荐答案
我将我的例子与
我发现只有一个区别,所有的区别:
I have compared my example with this oneAnd I've found only one difference that made all the difference:
v.setUseHashlookup(true); //v is the TreeViewer
文档中说明的方法如下:
What the method states in its doc:
现在内容提供者实际上是懒惰的。只能画出可见物品,而不是全部。
Now the content provider is actually lazy. Only paints the visible items, not ALL of them.
这篇关于Eclipse RCP - ILazyTreeContentProvider实现是意外的渴望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!