在使用 JFoenix 控件JFXListView
时,如果我使用单元工厂,则无法将材质样式分配给列表。如何使用单元工厂并同时指定JFXListView
控件的样式?
这是我的最小、完整和可验证的示例:
demoStyle.css(摘自 JFoenix 演示)
.jfx-list-cell-container {
-fx-alignment: center-left;
}
.jfx-list-cell-container > .label {
-fx-text-fill: BLACK;
}
.jfx-list-cell:odd:selected > .jfx-rippler > StackPane, .jfx-list-cell:even:selected > .jfx-rippler > StackPane {
-fx-background-color: rgba(0, 0, 255, 0.2);
}
.jfx-list-cell {
-fx-background-insets: 0.0;
-fx-text-fill: BLACK;
}
.jfx-list-cell:odd, .jfx-list-cell:even {
-fx-background-color: WHITE;
}
.jfx-list-cell:filled:hover {
-fx-text-fill: black;
}
.jfx-list-cell .jfx-rippler {
-jfx-rippler-fill: BLUE;
}
.jfx-list-view {
-fx-background-insets: 0;
-jfx-cell-horizontal-margin: 0.0;
-jfx-cell-vertical-margin: 5.0;
-jfx-vertical-gap: 10;
-jfx-expanded: false;
-fx-pref-width: 200;
}
demoLayout.fxml
<?import com.jfoenix.controls.JFXListView?>
<?import javafx.scene.layout.HBox?>
<HBox
xmlns="http://javafx.com/javafx/8.0.112"
xmlns:fx="http://javafx.com/fxml"
fx:controller="<some>.<valid>.<package>.<structure>.DemoController"
minWidth="Infinity"
minHeight="Infinity"
>
<JFXListView fx:id="jfoenixListViewPlain"/>
<JFXListView fx:id="jfoenixListViewWithCustomCell"/>
</HBox>
演示.kt
import com.jfoenix.controls.JFXListView
import javafx.application.Application
import javafx.collections.FXCollections
import javafx.fxml.FXML
import javafx.fxml.FXMLLoader
import javafx.fxml.Initializable
import javafx.scene.Scene
import javafx.scene.control.ContentDisplay
import javafx.scene.control.Label
import javafx.scene.control.ListCell
import javafx.scene.layout.HBox
import javafx.scene.paint.Color
import javafx.stage.Stage
import java.net.URL
import java.util.*
data class MyCustomData(val text: String)
class DemoController : Initializable
{
internal class MyCustomCell : ListCell<MyCustomData>()
{
init
{
text = null
contentDisplay = ContentDisplay.GRAPHIC_ONLY
}
override fun updateItem(item: MyCustomData?, empty: Boolean)
{
super.updateItem(item, empty)
graphic =
if (empty || item == null)
{
null
}
else
{
// In practice this isn't a Label but a Pane with multiple children
Label().apply { text = item.text }
}
}
}
@FXML
private lateinit var jfoenixListViewPlain: JFXListView<String>
@FXML
private lateinit var jfoenixListViewWithCustomCell: JFXListView<MyCustomData>
override fun initialize(location: URL?, resources: ResourceBundle?)
{
val plainList = FXCollections.observableArrayList("A", "B", "C")
jfoenixListViewPlain.items = plainList
val customItemsList = FXCollections.observableArrayList(MyCustomData("A"), MyCustomData("B"), MyCustomData("C"))
jfoenixListViewWithCustomCell.items = customItemsList
jfoenixListViewWithCustomCell.setCellFactory { MyCustomCell() }
}
}
class MainDemoClass : Application()
{
companion object
{
@JvmStatic
fun main(args: Array<String>)
{
Application.launch(MainDemoClass::class.java, *args)
}
}
@Throws(Exception::class)
override fun start(stage: Stage)
{
val loader = FXMLLoader(javaClass.getResource("/layouts/demoLayout.fxml"))
val root = loader.load<HBox>()
with(stage)
{
title = "Test's Title"
scene =
Scene(root, 600.0, 600.0, Color.WHITE).apply {
with(stylesheets)
{
add(MainDemoClass::class.java.getResource("/css/jfoenix-design.css").toExternalForm())
add(MainDemoClass::class.java.getResource("/css/demoStyle.css").toExternalForm())
}
}
isResizable = false
show()
}
}
}