开发者

Apache Pivot and custom ListView ItemRenderer

开发者 https://www.devze.com 2023-03-17 05:51 出处:网络
Say I want the following layout in my List View\'s items: <TablePane styles = \"{padding : 5, horizontalSpacing : 5, verticalSpacing : 5}\" >

Say I want the following layout in my List View's items:

<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>

    <TablePan开发者_开发百科e.Row height="-1">
        <TablePane.Filler/>
        <ActivityIndicator active="true" width="16" height="16"/>
    </TablePane.Row>
</TablePane>

And say I have the following custom ItemRenderer:

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

What is the best way to use the BXML snippet above with my custom ListRenderer?


The way I like to do this is to make CustomListRenderer be what you might call the "code behind" for a similarly-named bxml file, and add a factory method to it to create itself by calling the bxml serializer. Something like this:

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
}

Then put your snippet above into CustomListRenderer.bxml, but change the root item to be 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 ...

Finally, when you create the ListView that wants to use your custom renderer, you can do

listview.setItemRenderer(CustomListRenderer.create());
0

精彩评论

暂无评论...
验证码 换一张
取 消