我正在开发Eclipse RCP应用程序,并尝试使用ILazyTreeContentProvider
实现以便在特定时间仅显示可见项。
编码:
在扩展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内部:
@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个TreeItem可见。ILazyTreeContentProvider
不应该只加载可见的吗? (在这种情况下,这是7个项目)我一定做错了什么...
无论如何,任何意见都非常感谢!
最佳答案
我已经将我的示例与this one进行了比较
而且我发现只有一个与众不同的差异:
v.setUseHashlookup(true); //v is the TreeViewer
该方法在其文档中指出的内容:
现在,内容提供商实际上是懒惰的。仅绘制可见项目,而不是全部。
关于java - Eclipse RCP-ILazyTreeContentProvider实现异常急切,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19092744/