我正在使用nhaarman的ListviewAnimation库
https://github.com/nhaarman/ListViewAnimations,效果很好.
但是我面临以下问题:
我面临的主要问题是,我无法调试我的代码.我已经将四个必需的库直接复制/粘贴到libs文件夹中.在任何listview方法(如onItemLongClick())中放置调试点都不起作用.
第二个问题是,拖放listView在我的代码中不起作用.每当我尝试拖动任何列表项时,在删除列表项时,该项将采用与其拖动相同的位置.
这是我用过的代码:
listview.enableDragAndDrop(); listview.setDraggableManager(new TouchViewDraggableManager( R.id.list_row_draganddrop_textview)); listview.setOnItemMovedListener(this); listview.setOnItemLongClickListener(this); @Override public void onItemMoved(final int originalPosition,final int newPosition) { if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText(getApplicationContext(),"Moved" + swingBottomInAnimationAdapter.getItem(newPosition) + newPosition,Toast.LENGTH_SHORT); mToast.show(); } @Override public boolean onItemLongClick(final AdapterView<?> parent,final View view,final int position,final long id) { if (listview != null) { listview.startDragging(position - listview.getHeaderViewsCount()); } return true; }
解决方法
Whenever I try to drag any list item,on dropping the list item,the item takes the same position from which it was dragged.
当然.处理位置变化是你的责任,你应该在onItemMoved回调中处理它:
@Override public void onItemMoved(final int originalPosition,final int newPosition) { if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText(getApplicationContext(),"Moved" + swingBottomInAnimationAdapter.getItem(newPosition) + newPosition,Toast.LENGTH_SHORT); mToast.show(); // Adapt the following to your implementation if (originalPosition != newPosition) { YourObject item = (YourObject) yourAdapter.getItem(originalPosition); yourAdapter.moveItem(item,newPosition); } }
上面提到的方法看起来像:
public void moveItem(YourObject item,int newIndex) { if (mEntries != null) { mEntries.remove(item); mEntries.add(newIndex,item); notifyDataSetChanged(); } }
如果你浏览源代码,你会发现你正在拖动的是一个位图.列表项位于其原始位置.