Drag and drop does not work in ListviewAnimation nhaarman library

I am using the nhaarman ListviewAnimation library https://github.com/nhaarman/ListViewAnimations , which works great.

But I am having the following problems:

The main problem I am facing is that I cannot debug my code. I directly copied / pasted the four required libraries into the libs folder. Placing a debug point inside any of the listview methods, such as onItemLongClick() , does not work.

The second problem is that drag-drop listView does not work in my code. Whenever I try to drag any list item, when I delete a list item, the item takes the same position from which it was dragged.

Here is the code I used:

 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; } 
+6
source share
3 answers

Whenever I try to drag any list item, when I delete a list item, the item takes the same position from which it was dragged.

Sure. Handling a position change is your responsibility, and you should take care of this in the 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); } } 

The above method will look something like this:

 public void moveItem(YourObject item, int newIndex) { if (mEntries != null) { mEntries.remove(item); mEntries.add(newIndex, item); notifyDataSetChanged(); } } 

If you look at the source code, you will see that what you drag and drop is a Bitmap . The list item is in its original position.

+5
source

For others having the same issue, Niek Haarman answered this question on GitHub here .

I don’t see GitHub coming soon, but as a good tone to insert an answer, too, here it is:

 @Override public long getItemId(int position) { return position; } @Override public boolean hasStableIds() { return true; } 

position here is not stable. You need a stable identifier for an element that is position independent.

+5
source

using import com.nhaarman.listviewanimations.ArrayAdapter;

instead of import android.widget.ArrayAdapter;

that's why it doesn't call onItemMoved

0
source

Source: https://habr.com/ru/post/976042/


All Articles