How to drag and drop item from list to another item in list and interchange their positions

巧了我就是萌 提交于 2019-12-10 12:11:36

问题


I have a list field with multiple items. I want to select one item from list and drag and drop to another item in the list. After dropping the item on another item the position of items should be interchanged. (Example:- if i select 1st item from list and drag and drop this item to 4th item in list after drop that item position of both item should be changed 1st item on 4th position and 4th item on 1st position)

i don't know how to do this.Please help me to complete this task.

    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:


[UPDATED FOR BOTH TOUCH AND NON-TOUCH DEVICES]: Try the the following code; I've tested on 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);
    }
}

[EDITED:]

For touch-supported devices: a shadow of text of the first selected item moves along with the cursor.

For non-touch devices: I've used SPACEBAR to select the items to interchange. Select an item, press SPACEBAR, select the second item, press SPACEBAR again, items interchanged !! Pressing SPACEBAR on an already selected item will deselect that item.

Let me know whether it worked or not.

Further enhancement of list field customization has been posted in this StackOverflow Answer.



来源:https://stackoverflow.com/questions/10170144/how-to-drag-and-drop-item-from-list-to-another-item-in-list-and-interchange-thei

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!