【问题标题】:Drag and sort Listview OnItemClick issues android拖动和排序 Listview OnItemClick 问题 android
【发布时间】:2013-02-11 07:12:33
【问题描述】:

我正在使用拖动和排序列表视图来单击一个项目,但问题是当我对项目进行排序时,位置发生了变化。如何解决这个问题。我使用了这个例子。 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);
    }

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

activity.How 在拖放和排序列表视图中纠正这个问题?

提前致谢。

【问题讨论】:

  • 您在何时何地进行排序?

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


【解决方案1】:

您必须像这样检查 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;

                }
            }
        };

【讨论】:

  • 你能告诉我问题出在哪里吗?
  • 正如你所说,我已在 DropListener 中添加并运行它。但我仍然有问题。想要对 OnItemClickListener 进行任何更改吗?因为我正在使用 listview.setOnItemClickListener(this);所以它覆盖了 AdapterView。我怀疑的是,那个位置会检查 DropListener 吗?所以只有我认为我面临这个问题。
  • 首先告诉我这些项目在列表视图中可见或者您在向其中添加项目时遇到问题?
  • 项目在 listview dude 中可见。当我单击它时,它会显示相应项目的 toast 消息。但是当我排序并单击它时,它不匹配。
  • 您正在从数据库中挑选商品?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-05
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
  • 2014-07-02
  • 1970-01-01
相关资源
最近更新 更多