How to Re-Arrange ListField in Blackberry

后端 未结 2 1981
猫巷女王i
猫巷女王i 2021-01-26 05:28

How to re-arrange the items of a ListField? I want to select the item on click on Blackberry (Storm and Torch) Touch Phones. After select I want to (Move and Drag) Scroll item V

2条回答
  •  被撕碎了的回忆
    2021-01-26 05:57

    I was also working on custom ListField recently and I hope the following code is going to help you.

    In the example below, I've customized a VerticalFieldManager to create a ListField for both touch supported and non-touch BB devices.

    Position of the ListField items can be changed-

    -- by just DRAG an item and DROP on another item of the ListField (for touch supported devices)

    or

    -- by pressing SPACE-BAR on the first item and then on the second item (for both touch and non-touch devices).

    Sample screenshot for (touch) Drag & Drop below:

    Touch Supported

    Sample screenshot for (non-touch) SPACE-BAR pressing below:

    Non Touch

    In this sample I've implemented vertical scrolling in the ListField and have also created a traditional scrollbar.

    Here is the code of my sample screen for ListField customization:

    import net.rim.device.api.system.Characters;
    import net.rim.device.api.ui.Field;
    import net.rim.device.api.ui.Font;
    import net.rim.device.api.ui.Graphics;
    import net.rim.device.api.ui.TouchEvent;
    import net.rim.device.api.ui.XYEdges;
    import net.rim.device.api.ui.component.LabelField;
    import net.rim.device.api.ui.component.ListField;
    import net.rim.device.api.ui.component.ListFieldCallback;
    import net.rim.device.api.ui.component.ObjectListField;
    import net.rim.device.api.ui.container.MainScreen;
    import net.rim.device.api.ui.container.VerticalFieldManager;
    import net.rim.device.api.ui.decor.BackgroundFactory;
    import net.rim.device.api.ui.decor.Border;
    import net.rim.device.api.ui.decor.BorderFactory;
    
    public class ListTestScreen extends MainScreen {
        private VerticalFieldManager mainManager = new VerticalFieldManager( USE_ALL_WIDTH |FIELD_HCENTER);
        public ListTestScreen() {
            mainManager.setBackground(BackgroundFactory.createSolidBackground(0x808080));
            LabelField lblTitle = new LabelField("FIFA World Cup Country List:",FIELD_HCENTER);
            lblTitle.setBackground(BackgroundFactory.createSolidBackground(0xF0F0F0));
            XYEdges edges = new XYEdges(2, 2, 2, 2);
            lblTitle.setBorder(BorderFactory.createRoundedBorder(edges, 0xB0B0B0, Border.STYLE_FILLED));
            mainManager.add(lblTitle);
            mainManager.add(new MyList());
            LabelField lblOther = new LabelField("Some Other Field",FIELD_HCENTER);
            lblOther.setBackground(BackgroundFactory.createSolidBackground(0xF0F0F0));
            lblOther.setBorder(BorderFactory.createRoundedBorder(edges, 0xB0B0B0, Border.STYLE_FILLED));
            mainManager.add(lblOther);
            add(mainManager);
        }
    }
    
    class MyList extends VerticalFieldManager implements ListFieldCallback {
        private int  mouseDownY, mouseUpY;
        private static int listRowHeight = 30;
        private int mouseDownRowIndex, mouseUpRowIndex;
        int firstSelectedRow=-1, secondSelectedRow=-1;
        private int touchX;
        private int touchY;
        private int listVisibleHeight = 250;
        private int listVisibleWidth = 200;
    
        private boolean showShadow = false;
    
        private ObjectListField list;
        private Object[] listData = new Object[] { "South Africa", "Argentina", "Germany",
                "Australia","Nigeria","Greece","England","Italy","Brazil","Spain","Paraguay","France","Uruguay",
                "Mexico","Cameroon","Denmark","Portugal","Netherlands","Ghana","Chile",
                "South Korea","USA","Algeria","Slovenia","Japan","Switzerland","Honduras"};
    
        public MyList() {
            super(VERTICAL_SCROLL|FIELD_HCENTER);
            init();
            setBackground(BackgroundFactory.createSolidBackground(0xF0F0F0));
            XYEdges edges = new XYEdges(6, 6, 6, 6);
            setBorder(BorderFactory.createRoundedBorder(edges, 0xB0B0B0, Border.STYLE_FILLED));
        }
    
        private void init() {
            list = new ObjectListField(FOCUSABLE|Field.FIELD_HCENTER);
            list.setRowHeight(listRowHeight);
            list.setCallback(this);
            list.set(listData);
            add(list);
        }
    
        private void changeRowPosition(int fromRow, int toRow) {
            Object temp = listData[fromRow];
            int increment = (fromRow 0) {
                        index--;
                    }
                } else if(touchY> (getPreferredHeight()-5)) {
                    if(index

提交回复
热议问题