如何将项目从列表拖放到列表中的另一个项目并交换其位置

时间:2012-04-16 07:26:35

标签: blackberry listfield

我有一个包含多个项目的列表字段。我想从列表中选择一个项目并拖放到列表中的另一个项目。将项目放在另一个项目上后,项目的位置应该互换。 (示例: - 如果我从列表中选择第一项并将此项拖放到列表中的第4项  之后,两个项目的项目位置应该改变第4个位置的第1个项目和第1个位置的第4个项目)

我不知道该怎么做。请帮我完成这项任务。

    public class ListScreen extends MainScreen implements ListFieldCallback {
    // private ObjectChoiceField choices;
    private int x, y;
    int start = 0 , end=0;
    private int startPos;
//  private int pos;
    private ObjectListField list;
    private Object[] listData = new Object[] { "Ram", "Sunny", "Ramit",
            "Vishal","Ravinder","Manoj","Ravi","Vikas","Santosh","Ravi","Deepak","Sharma","Nvn" };

    public ListScreen() {
        init();
    }

    private void init() {
        list = new ObjectListField(FOCUSABLE);
        list.setRowHeight(45);
        list.setCallback(this);
        list.set(listData);
        add(list);
    }

    public void drawListRow(ListField listField, Graphics graphics, int index,
            int y, int width) {
        graphics.drawText(listData[index].toString(), 25, y + 10);

    }

    public Object get(ListField listField, int index) {
        return listData[index];
    }

    public int getPreferredWidth(ListField listField) {
        return getPreferredWidth();
    }

    public int indexOfList(ListField listField, String prefix, int start) {
        // TODO Auto-generated method stub
        return 0;
    }

    // protected boolean navigationMovement(int dx, int dy, int status, int
    // time) {
    //
    // return super.navigationMovement(dx, dy, status, time);
    // }

    protected boolean touchEvent(TouchEvent message) {
        int eventCode = message.getEvent();

        // Get the screen coordinates of the touch event
        int touchX = message.getX(1);
        int touchY = message.getY(1);
        if (eventCode == TouchEvent.CLICK) {


            calculateStart(touchY);
            // Dialog.alert("You clicked at (" + touchX + "," + touchY + ")");
        }

        if (eventCode == TouchEvent.MOVE) {

            x = touchX;
            y = touchY;

            invalidate();
        }
        if (eventCode == TouchEvent.UNCLICK) {
            int unClickx = message.getX(1);
            int unClicky = message.getY(1);
            System.out.println("unclick at:" + unClickx + "::::" + unClicky);
            calculatePosition(unClicky);
        }



        return true;
    }


    private void calculatePosition(int dy) {
        if (dy != 0) {

                end = dy / 45;
                System.out.println(start + ":::" + end);
                Object temp = listData[start];
                listData[start] = listData[end];
                listData[end] = temp;
                invalidate();

        }

    }
    private void calculateStart(int dy){
        if(dy!=0)
        start = dy/45;

    }
}

1 个答案:

答案 0 :(得分:3)

[ 更新了触摸和非触摸设备 ]:尝试以下代码;我在Torch(9800),Storm(9550),Bold(9700)上测试过:

import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
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;

public class ListTestScreen extends MainScreen {
    private VerticalFieldManager mainManager = new VerticalFieldManager();
    public ListTestScreen() {
        mainManager.add(new LabelField("My Title"));
        mainManager.add(new LabelField("My List:"));
        mainManager.add(new MyList());
        mainManager.add(new LabelField("Something else"));
        add(mainManager);
    }
}

class MyList extends VerticalFieldManager implements ListFieldCallback {
    private int  mouseDownY, mouseUpY;
    private static int rowHeight = 45;
    private int mouseDownRowIndex, mouseUpRowIndex;
    int firstSelectedRow=-1, secondSelectedRow=-1;
    private int touchX;
    private int touchY;
    private boolean showShadow = false;

    private ObjectListField list;
    private Object[] listData = new Object[] { "Ram", "Sunny", "Ramit",
            "Vishal","Ravinder","Manoj","Ravi","Vikas","Santosh","Ravi","Deepak","Sharma","Nvn" };

    public MyList() {
        init();
        setBackground(BackgroundFactory.createLinearGradientBackground(0x0FCFC0, 0x0CCCC0, 0x09C9C0, 0x01C1C0));
    }

    private void init() {
        list = new ObjectListField(FOCUSABLE);
        list.setRowHeight(rowHeight);
        list.setCallback(this);
        list.set(listData);
        add(list);
    }

    private void interchangeRows(int r1, int r2) {
        Object temp = listData[r1];
        listData[r1] = listData[r2];
        listData[r2] = temp;
        invalidate();
    }

    public void drawListRow(ListField listField, Graphics graphics, int index,
            int y, int width) {
        graphics.drawText(listData[index].toString(), 25, y + 10);

    }

    public Object get(ListField listField, int index) {
        return listData[index];
    }

    public int getPreferredWidth(ListField listField) {
        return getPreferredWidth();
    }

    public int indexOfList(ListField listField, String prefix, int start) {
        // TODO Auto-generated method stub
        return 0;
    }

    public int indexOfRowAt(int posY) {
        int index =(int) Math.floor(posY / rowHeight * 1.0); 
        return index;
    }


    protected boolean keyChar(char ch, int status, int time) {
        if(ch==Characters.SPACE) {
            if(firstSelectedRow ==-1) {
                firstSelectedRow = list.getSelectedIndex();
                return true;
            } else {
                secondSelectedRow = list.getSelectedIndex();
                if(firstSelectedRow == secondSelectedRow) {
                    firstSelectedRow = secondSelectedRow = -1;
                    invalidate();
                    return true;
                } else {
                    interchangeRows(firstSelectedRow, secondSelectedRow);
                    firstSelectedRow = secondSelectedRow = -1;
                    return true;
                }
            }
        }
        return super.keyChar(ch, status, time);
    }

    protected void paint(Graphics graphics) {
        if(firstSelectedRow!=-1) {
            int x = 0, y = firstSelectedRow*rowHeight;
            int savedColor = graphics.getColor();
            int preAlpha = graphics.getGlobalAlpha();
            graphics.setGlobalAlpha(90) ;           
            graphics.setColor(0xFF22FF);

            graphics.fillRect(0, y, getWidth(), rowHeight);


            graphics.setColor(savedColor);
            graphics.setGlobalAlpha(preAlpha) ;
        }

        super.paint(graphics);

        if(showShadow && mouseDownRowIndex != -1) {
            int preAlpha = graphics.getGlobalAlpha();
            graphics.setGlobalAlpha(100) ;
            graphics.drawText(listData[mouseDownRowIndex].toString(), 25, touchY);
            graphics.setGlobalAlpha(preAlpha) ;
        }
    }

    protected boolean touchEvent(TouchEvent message) {
        int eventCode = message.getEvent();

        // Get the screen coordinates of the touch event
        touchX = message.getX(1);
        touchY = message.getY(1);       


        if(eventCode == TouchEvent.DOWN) {
            mouseDownY = touchY;
            mouseDownRowIndex = indexOfRowAt(mouseDownY);
            showShadow = true;
            invalidate();
            return true;
        }        
        else if(eventCode == TouchEvent.UP) {
            showShadow = false;
            mouseUpY = touchY;
            mouseUpRowIndex = indexOfRowAt(mouseUpY);
            if(mouseDownRowIndex != mouseUpRowIndex) {
                interchangeRows(mouseDownRowIndex, mouseUpRowIndex);
                mouseDownRowIndex = mouseUpRowIndex = -1;
                return true;
            }
            mouseDownRowIndex = mouseUpRowIndex = -1;
            invalidate();
        } else if(eventCode == TouchEvent.MOVE) {
            invalidate();
            return true;
        }
        return super.touchEvent(message);
    }
}

[<强>编辑:

对于支持触控的设备:第一个选定项目的文字阴影会随光标一起移动。

对于非触控设备:我使用空格键来选择要互换的项目。选择一个项目,按空格键,选择第二个项目,再次按空格键,项目互换!!在已选择的项目上按空格键将取消选择该项目。

让我知道它是否有效。

  

列表字段自定义的进一步增强已发布在中   这StackOverflow Answer