在努力工作并基于@warakawa 答案后,我找到了解决方案。众所周知,Listview的实现让很多人头疼,因为这个我写了两个类来解决“PREF_SIZE”的问题(它有一个常数400的高度和宽度是根据高度计算的)。我编写的皮肤类会按照我们的预期计算大小,并且当新属性“fillWidth”更改为“true”时,还可以防止丑陋的水平条,该属性使单元格尽可能水平地增长。问候。
ListViewFixed.java
package javafxapplication;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.scene.control.Skin;
public class ListViewFixed<T> extends javafx.scene.control.ListView<T>
{
// <editor-fold defaultstate="collapsed" desc="Properties">
private final BooleanProperty fillWidth = new SimpleBooleanProperty(this, "fillWidth");
public final BooleanProperty fillWidthProperty()
{
return fillWidth;
}
public final boolean isFillWidth()
{
return fillWidth.get();
}
public final void setFillWidth(boolean fillWidth)
{
this.fillWidth.set(fillWidth);
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Methods">
@Override
protected Skin createDefaultSkin()
{
return new ListViewFixedSkin(this);
}
// </editor-fold>
}
ListViewFixedSkin.java
package javafxapplication;
import java.util.Set;
import javafx.beans.Observable;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.IndexedCell;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.Region;
public class ListViewFixedSkin extends com.sun.javafx.scene.control.skin.ListViewSkin
{
// <editor-fold defaultstate="collapsed" desc="Fields">
private ListViewFixed listView;
private ScrollBar scrollBarHorizontal;
private ScrollBar scrollBarVertical;
private boolean fillWidthCache;
private double prefWidthCache;
private Region placeholderRegion;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Constructors">
public ListViewFixedSkin(ListViewFixed listView)
{
super(listView);
this.listView = listView;
registerChangeListener(listView.fillWidthProperty(), "FILL_WIDTH");
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Methods">
private void updateFillWidth()
{
if (scrollBarHorizontal != null && scrollBarVertical != null && fillWidthCache != listView.isFillWidth())
{
if (listView.isFillWidth() && !fillWidthCache)
{
scrollBarHorizontal.visibleProperty().addListener(this::updateCellsPrefWidth);
scrollBarVertical.visibleProperty().addListener(this::updateCellsPrefWidth);
}
else
{
scrollBarHorizontal.visibleProperty().removeListener(this::updateCellsPrefWidth);
scrollBarVertical.visibleProperty().removeListener(this::updateCellsPrefWidth);
}
fillWidthCache = listView.isFillWidth();
}
}
private void updateCellsPrefWidth(Observable o)
{
final Insets insets = getSkinnable().getInsets();
final double prefWidth = getSkinnable().getWidth() + insets.getLeft() + insets.getRight() - scrollBarVertical.getWidth();
if (prefWidth != prefWidthCache)
{
for (int i = 0; i < flow.getCellCount(); i++)
{
final IndexedCell cell = flow.getCell(i);
if (!cell.isEmpty())
{
cell.setPrefWidth(prefWidth);
}
}
prefWidthCache = prefWidth;
}
}
private boolean showingPlaceHolder()
{
checkState();
if (getItemCount() == 0)
{
if (placeholderRegion == null)
{
updatePlaceholderRegionVisibility();
final Object obj = getChildren().get(getChildren().size() - 1);
if (obj instanceof Node && ((Region) obj).getStyleClass().contains("placeholder"))
{
placeholderRegion = (Region) obj;
}
}
if (placeholderRegion != null)
{
return true;
}
}
return false;
}
@Override
protected void handleControlPropertyChanged(String p)
{
super.handleControlPropertyChanged(p);
if ("FILL_WIDTH".equals(p))
{
updateFillWidth();
}
}
@Override
protected double computePrefHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset)
{
if (showingPlaceHolder())
{
return super.computePrefHeight(width, topInset, rightInset, bottomInset, leftInset);
}
else
{
double computedHeight = topInset + bottomInset;
for (int i = 0; i < flow.getCellCount(); i++)
{
final IndexedCell cell = flow.getCell(i);
if (!cell.isEmpty())
{
computedHeight += cell.getHeight();
}
}
return computedHeight;
}
}
@Override
protected double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset)
{
double computedWidth = 0;
if (showingPlaceHolder())
{
computedWidth += placeholderRegion.getLayoutBounds().getWidth();
}
else
{
for (int i = 0; i < flow.getCellCount(); i++)
{
final IndexedCell cell = flow.getCell(i);
if (!cell.isEmpty() && cell.getWidth() > computedWidth)
{
computedWidth = cell.getWidth();
}
}
if (scrollBarVertical != null && scrollBarVertical.isVisible())
{
computedWidth += scrollBarVertical.getWidth();
}
}
if (computedWidth != 0)
{
return computedWidth + leftInset + rightInset;
}
else
{
return super.computePrefWidth(height, topInset, rightInset, bottomInset, leftInset);
}
}
@Override
protected void layoutChildren(double x, double y, double w, double h)
{
super.layoutChildren(x, y, w, h);
if (scrollBarHorizontal == null || scrollBarVertical == null)
{
final Set<Node> nodes = getSkinnable().lookupAll(".scroll-bar");
nodes.stream().forEach((node) ->
{
if (node instanceof ScrollBar)
{
final ScrollBar scrollBar = (ScrollBar) node;
if (scrollBar.getOrientation() == Orientation.HORIZONTAL)
{
scrollBarHorizontal = scrollBar;
}
else
{
scrollBarVertical = scrollBar;
}
}
});
updateFillWidth();
}
}
@Override
public void dispose()
{
if (fillWidthCache)
{
scrollBarHorizontal.visibleProperty().removeListener(this::updateCellsPrefWidth);
scrollBarVertical.visibleProperty().removeListener(this::updateCellsPrefWidth);
}
listView = null;
super.dispose();
}
// </editor-fold>
}
警告
ListViewFixedSkin.java 类有一些修复工作要做。在我直接通过引用传递方法的地方,它应该先存储在变量中,然后在添加/附件/删除/分离方法中传递变量。否则会导致内存问题。