有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Apache Pivot和自定义ListView ItemRenderer

假设我希望在列表视图的项目中使用以下布局:

<TablePane styles = "{padding : 5, horizontalSpacing : 5, verticalSpacing : 5}" >
    <columns>
        <TablePane.Column width = "1*"/>
        <TablePane.Column width = "21"/>
    </columns>

    <TablePane.Row height="-1">
        <TextInput bxml:id ="txtName" 
                   textSize="15" />
        <TablePane.Filler/>
    </TablePane.Row>

    <TablePane.Row height="-1">
        <TablePane.Filler/>
        <ActivityIndicator active="true" width="16" height="16"/>
    </TablePane.Row>
</TablePane>

假设我有以下自定义ItemRenderer:

public class CustomListRenderer extends TablePane
                                implements ListView.ItemRenderer {
    // stuff here
}

在自定义ListRenderer中使用上述BXML片段的最佳方法是什么


共 (1) 个答案

  1. # 1 楼答案

    我喜欢这样做的方式是使CustomListRenderer成为类似命名的bxml文件的“代码隐藏”,并向其添加一个工厂方法,通过调用bxml序列化程序来创建自己。大概是这样的:

    public class CustomListRenderer extends TablePane
                                    implements ListView.ItemRenderer {
        public static CustomListRenderer create() throws IOException, SerializationException {
        BXMLSerializer bxmlSerializer = new BXMLSerializer();
        return (CustomListRenderer) bxmlSerializer.readObject(CustomListRenderer.class, "CustomListRenderer.bxml");
        }
    
        // rest of your stuff here
    }
    

    然后将上面的代码片段放入CustomListRenderer。bxml,但将根项更改为CustomListRenderer:

    <my:CustomListRenderer styles = "{padding : 5, horizontalSpacing : 5, verticalSpacing : 5}" 
      xmlns:bxml="http://pivot.apache.org/bxml"
      xmlns:my="your.java.package.here"
      xmlns="org.apache.pivot.wtk">
      ... rest of your bxml here ...
    

    最后,在创建要使用自定义渲染器的ListView时,可以执行以下操作:

    listview.setItemRenderer(CustomListRenderer.create());