任何类似 recyclerview 或 javafx 的可重用视图
Anything like recyclerview or reusable views for javafx
我正在创建一个滚动窗格,它显示从 sqlite 数据库中获取的数据集,这些数据的显示方式是它们排列在 ui 的重复集中,就像在 [=12 中的 recyclerview 中一样=].
有没有办法实现它,因为使用 javafx
定位元素并不容易
有几个 classes 在 javafx 中使用类似的概念:
适配器被 cellFactory
(s) 替换,位置实际上并没有等效项,但单元格确实包含 index
属性。 Cell
class中有一个updateItem
方法负责更新cell visuals.
请使用一些教程来熟悉这些控件,因为这超出了答案的范围。
ListCell
的工作方式类似于 TableView
,但基本上只有一列,而 cellFactory
是 ListView
[的 属性 class 本身,没有 cellValueFactory
等价物。
TreeView
和 TreeTableView
的工作方式分别与 ListView
和 TableView
非常相似,主要区别在于支持分层数据结构。
我会实现自己的 ListCell,然后在 ListView 中使用它。
它会是这样的:
public class RecyclerLikeCell extends ListCell<MyCustomBean> {
@Override
protected void updateItem(MyCustomBean item, boolean empty){
super.updateItem(item, empty);
//Create whatever you need to render. And then add:
//I'm using a VBox as an example, you may use whatever you need.
var myComponent = new VBox(component1, component2, component3);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setGraphic(myComponent);
}
/**
* Utility method to use as ListCellFactory.
*
* @return a Callback to use in ListView.
*/
public static Callback<ListView<MyCustomBean>, ListCell<MyCustomBean>>
forListView(){
return i -> new RecyclerLikeCell();
}
}
然后,在您的控制器初始化中,您可以使用:
listRecyclerView.setCellFactory(RecyclerLikeCell.forListView());
而且应该没问题。我用它创建了一个用户菜单,就像 SAP Business One GUI 上的菜单一样。
我正在创建一个滚动窗格,它显示从 sqlite 数据库中获取的数据集,这些数据的显示方式是它们排列在 ui 的重复集中,就像在 [=12 中的 recyclerview 中一样=]. 有没有办法实现它,因为使用 javafx
定位元素并不容易有几个 classes 在 javafx 中使用类似的概念:
适配器被 cellFactory
(s) 替换,位置实际上并没有等效项,但单元格确实包含 index
属性。 Cell
class中有一个updateItem
方法负责更新cell visuals.
请使用一些教程来熟悉这些控件,因为这超出了答案的范围。
ListCell
的工作方式类似于 TableView
,但基本上只有一列,而 cellFactory
是 ListView
[的 属性 class 本身,没有 cellValueFactory
等价物。
TreeView
和 TreeTableView
的工作方式分别与 ListView
和 TableView
非常相似,主要区别在于支持分层数据结构。
我会实现自己的 ListCell,然后在 ListView 中使用它。 它会是这样的:
public class RecyclerLikeCell extends ListCell<MyCustomBean> {
@Override
protected void updateItem(MyCustomBean item, boolean empty){
super.updateItem(item, empty);
//Create whatever you need to render. And then add:
//I'm using a VBox as an example, you may use whatever you need.
var myComponent = new VBox(component1, component2, component3);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
setGraphic(myComponent);
}
/**
* Utility method to use as ListCellFactory.
*
* @return a Callback to use in ListView.
*/
public static Callback<ListView<MyCustomBean>, ListCell<MyCustomBean>>
forListView(){
return i -> new RecyclerLikeCell();
}
}
然后,在您的控制器初始化中,您可以使用:
listRecyclerView.setCellFactory(RecyclerLikeCell.forListView());
而且应该没问题。我用它创建了一个用户菜单,就像 SAP Business One GUI 上的菜单一样。