我正在开发一个 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++);
尽管只有 7 个树项可见,但该命令会打印出所有 22 个子元素。
不应该ILazyTreeContentProvider
唯一加载可见的吗?(在这种情况下,它是 7 个项目)
我一定做错了什么...
无论如何,任何意见都非常感谢!