0

我正在使用拖动和排序列表视图来单击一个项目,但问题是当我对项目进行排序时,位置发生了变化。如何解决这个问题。我使用了这个例子。https://github.com/commonsguy/cwac-touchlist。我的编码如下:

Java 类使用DragNDropListView

ListView listView = getListView();

    listView.setOnItemClickListener(this);



    if (listView instanceof DragNDropListView) {
        ((DragNDropListView) listView).setDropListener(mDropListener);
        ((DragNDropListView) listView).setRemoveListener(mRemoveListener);
        ((DragNDropListView) listView).setDragListener(mDragListener);

    }
}

private DropListener mDropListener = 
    new DropListener() {
    public void onDrop(int from, int to) {
        ListAdapter adapter = getListAdapter();
        if (adapter instanceof DragNDropAdapter) {
            ((DragNDropAdapter)adapter).onDrop(from, to);
            getListView().invalidateViews();
        }
    }
};

private RemoveListener mRemoveListener =
    new RemoveListener() {
    public void onRemove(int which) {
        ListAdapter adapter = getListAdapter();
        if (adapter instanceof DragNDropAdapter) {
            ((DragNDropAdapter)adapter).onRemove(which);
            getListView().invalidateViews();
        }
    }
};

private DragListener mDragListener =
    new DragListener() {

    int backgroundColor = 0xe0103010;
    int defaultBackgroundColor;

        public void onDrag(int x, int y, ListView listView) {
            // TODO Auto-generated method stub
        }

        public void onStartDrag(View itemView) {
            itemView.setVisibility(View.VISIBLE);
            defaultBackgroundColor = itemView.getDrawingCacheBackgroundColor();
            itemView.setBackgroundColor(backgroundColor);
            ImageView iv = (ImageView)itemView.findViewById(R.id.ImageView01);
            if (iv != null) iv.setVisibility(View.VISIBLE);
        }

        public void onStopDrag(View itemView) {
            itemView.setVisibility(View.VISIBLE);
            itemView.setBackgroundColor(defaultBackgroundColor);
            ImageView iv = (ImageView)itemView.findViewById(R.id.ImageView01);
            if (iv != null) iv.setVisibility(View.VISIBLE);
        }

};

private static String[] mListContent={"Item 1", "Item 2", "Item 3"};

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    // TODO Auto-generated method stub
    if(position==0)
    {
        Toast.makeText(this, "You have selected item 1",Toast.LENGTH_SHORT)
        .show();
    }

    if(position==1)
    {
        Toast.makeText(this, "Item 2",Toast.LENGTH_SHORT)
        .show();
    }

    if(position==2)
    {
    Intent i=new Intent(a.this,b.class);
           startActivity(i);
    }

但是在排序时,位置肯定会发生变化,所以它不能使用意图传递给下一个

活动。如何在拖动和排序列表视图中纠正这个问题?

提前致谢。

4

1 回答 1

0

您必须像这样检查 DropListener 中的位置:

private int position = 0;

private DropListener mDropListener = new DropListener() {
            public void onDrop(int from, int to) {
                ListAdapter adapter = listView.getAdapter();
                if (adapter instanceof DragNDropAdapter) {
                    ((DragNDropAdapter) adapter).onDrop(from, to);
                    listView.invalidateViews();
                    if(position == from)
                        position = to;
                    else if(position == to && ((from - to) == 1 || (to - from) == 1))
                        position = from;
                    else if(position == to && (from - to) > 1)
                        position = (from - to)-1;
                    else if(position == to && (to - from) > 1)
                        position = (to - from)-1;
                    else if(position > from && position < to)
                        position -= 1;
                    else if(position < from && position >to)
                        position += 1;

                }
            }
        };
于 2013-02-11T07:25:43.727 回答