提问者:小点点

任何类似于recyclerview或javafx的可重用视图的东西


我正在创建一个滚动窗格,它显示从sqlite db获取的数据集,并且这些数据以这样一种方式显示,即它们以重复的UI集排列,就像在Android中的recyclerview中一样。有没有办法实现它,因为使用 javafx 定位元素并不容易


共2个答案

匿名用户

在javafx中有几个类可以使用类似的概念:

  • 列表查看
  • 表格视图
  • 树视图
  • 树表视图

适配器被cell Factory(s)替换,位置并不真正具有等效项,但单元格确实包含index属性。Cell类中有一个updateItem方法负责更新单元格视觉对象。

请使用一些教程来熟悉这些控件,因为这超出了答案的范围。

ListCell的工作方式与TableView类似,但基本上只有一个列,cellFactoryListView类本身的属性,没有cellValueFactory等效项。

TreeViewTreeTableViewListViewTableView非常相似,主要区别在于支持分层数据结构。

匿名用户

我会实现我自己的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上的菜单。