拖动和排序Listview OnItemClick问题android

时间:2013-02-11 07:12:34

标签: android android-layout android-intent android-widget android-listview

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

使用DragNDropListView

的Java类
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);
    }

但是在排序时,肯定会有不同的位置,所以我无法使用意图传递给下一个

活动。如何在拖放列表视图中排除此问题?

提前致谢。

1 个答案:

答案 0 :(得分: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;

                }
            }
        };