Wednesday, July 11, 2012

List - ListButton

ListButton在畫面上會呈現下拉式的選單,本範例是使用者點選ListButton後,出現下拉式選單,當使用者選擇文字後,會在右方顯示圖片。本範例由官網取得。




============範例展示=================================================


============範例展示=================================================

<!--ui.bxml-->
<myex:ListButtons title="List Buttons" maximized="true"
                  xmlns:bxml="http://pivot.apache.org/bxml"
                  xmlns:myex="myex"
                  xmlns="org.apache.pivot.wtk">
    <TablePane styles="{showHorizontalGridLines: true, showVerticalGridLines:true,
        horizontalSpacing:1, verticalSpacing:1}">
        <columns>
            <TablePane.Column width="-1"/>
            <TablePane.Column width="1*"/>
        </columns>
 
        <TablePane.Row height="340">
            <BoxPane orientation="vertical" styles="{verticalAlignment:'top', padding: 4}">
                <Label text="圖片:"/>
                <ListButton bxml:id="listButton"
                            listData="['IMG_0725_2.jpg', 'IMG_0735_2.jpg', 'IMG_0767_2.jpg', 'IMG_1147.jpg']"/>
            </BoxPane>
 
            <ImageView bxml:id="imageView" styles="{backgroundColor:'#404040'}"/>
        </TablePane.Row>
    </TablePane>
</myex:ListButtons>


接下來則是myex#ListButtons.java。

//ListButtons.java
package myex;
import java.net.URL;
import org.apache.pivot.beans.Bindable;
import org.apache.pivot.collections.Map;
import org.apache.pivot.util.Resources;
import org.apache.pivot.util.concurrent.TaskExecutionException;
import org.apache.pivot.wtk.*;
import org.apache.pivot.wtk.media.Image;
 
public class ListButtons extends Window implements Bindable {
    private ListButton listButton = null;
    private ImageView imageView = null;
 
    @Override
    public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
        listButton = (ListButton)namespace.get("listButton");
        imageView = (ImageView)namespace.get("imageView");
 
        //實作ListButtonSelection事件處理機制
        listButton.getListButtonSelectionListeners().add(new ListButtonSelectionListener.Adapter() {
            @Override
            public void selectedItemChanged(ListButton listButton, Object previousSelectedItem) {
                Object selectedItem = listButton.getSelectedItem();
 
                if (selectedItem != null) {
                    //使用ClassLoader載入相關資源
                    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
                    URL imageURL = classLoader.getResource("myex/" + selectedItem);
 
                    //如果圖片尚未加入快取,就加入ResourceCache中
                    Image image = (Image)ApplicationContext.getResourceCache().get(imageURL);
 
                    if (image == null) {
                        try {
                            image = Image.load(imageURL);
                        } catch (TaskExecutionException exception) {
                            throw new RuntimeException(exception);
                        }
 
                        ApplicationContext.getResourceCache().put(imageURL, image);
                    }
 
                    // Update the image
                    imageView.setImage(image);
                }
            }
        });
 
        listButton.setSelectedIndex(0);
    }
}

No comments:

Post a Comment