ListView的可见项
Visible items of ListView
有没有办法获取 ListView
在 JavaFX
中的可见项?我想确定 ListView
在我的 JavaFX
应用程序中显示的第一个可见项。
以下代码 found here 对我不起作用(仅适用于 TableView
):
@SuppressWarnings("restriction")
public void getFirstAndLast(ListView<?> t) {
try {
ListViewSkin<?> ts = (ListViewSkin<?>) t.getSkin();
VirtualFlow<?> vf = (VirtualFlow<?>) ts.getChildren().get(0);
first = vf.getFirstVisibleCell().getIndex();
last = vf.getLastVisibleCell().getIndex();
}catch (Exception ex) {}
}
public int getFirst() {
return first;
}
public int getLast() {
return last;
}
正如@James_D 所解释的那样,没有一个很好的开箱即用的解决方案,但只有 hack 才能奏效。我改编了一个我在网上找到的解决方案,如下所示。
[...]
private int first = 0;
private int last = 0;
public void getFirstAndLast(ListView<?> t) {
try {
ListViewSkin<?> ts = (ListViewSkin<?>) t.getSkin();
VirtualFlow<?> vf = (VirtualFlow<?>) ts.getChildren().get(0);
first = vf.getFirstVisibleCell().getIndex();
last = vf.getLastVisibleCell().getIndex();
logger.debug("##### Scrolling first {} last {}", first, last);
} catch (Exception ex) {
logger.debug("##### Scrolling: Exception " + ex);
}
}
public int getFirst() {
return first;
}
public int getLast() {
return last;
}
[...]
示例输出:
13:56:38.652 [X Application Thread] DEBUG getFirstAndLast -
Scrolling #### first 11 last 20 13:56:48.503 [X Application Thread]
DEBUG getFirstAndLast - Scrolling #### first 9 last 17
13:57:08.491 [X Application Thread] DEBUG getFirstAndLast -
Scrolling #### first 7 last 15 13:57:18.371 [X Application Thread]
DEBUG getFirstAndLast - Scrolling #### first 3 last 15
有没有办法获取 ListView
在 JavaFX
中的可见项?我想确定 ListView
在我的 JavaFX
应用程序中显示的第一个可见项。
以下代码 found here 对我不起作用(仅适用于 TableView
):
@SuppressWarnings("restriction")
public void getFirstAndLast(ListView<?> t) {
try {
ListViewSkin<?> ts = (ListViewSkin<?>) t.getSkin();
VirtualFlow<?> vf = (VirtualFlow<?>) ts.getChildren().get(0);
first = vf.getFirstVisibleCell().getIndex();
last = vf.getLastVisibleCell().getIndex();
}catch (Exception ex) {}
}
public int getFirst() {
return first;
}
public int getLast() {
return last;
}
正如@James_D 所解释的那样,没有一个很好的开箱即用的解决方案,但只有 hack 才能奏效。我改编了一个我在网上找到的解决方案,如下所示。
[...]
private int first = 0;
private int last = 0;
public void getFirstAndLast(ListView<?> t) {
try {
ListViewSkin<?> ts = (ListViewSkin<?>) t.getSkin();
VirtualFlow<?> vf = (VirtualFlow<?>) ts.getChildren().get(0);
first = vf.getFirstVisibleCell().getIndex();
last = vf.getLastVisibleCell().getIndex();
logger.debug("##### Scrolling first {} last {}", first, last);
} catch (Exception ex) {
logger.debug("##### Scrolling: Exception " + ex);
}
}
public int getFirst() {
return first;
}
public int getLast() {
return last;
}
[...]
示例输出:
13:56:38.652 [X Application Thread] DEBUG getFirstAndLast - Scrolling #### first 11 last 20 13:56:48.503 [X Application Thread] DEBUG getFirstAndLast - Scrolling #### first 9 last 17 13:57:08.491 [X Application Thread] DEBUG getFirstAndLast - Scrolling #### first 7 last 15 13:57:18.371 [X Application Thread] DEBUG getFirstAndLast - Scrolling #### first 3 last 15