我正在實現類似的東西。 希望這會幫助別人,因爲我仍然在糾結。
...
thumb.setOnDragListener(new Drag());
...
public class Drag implements OnDragListener{
public static int PositionOfDraggedItem = -1;
public int PositionOfItemDraggedOver = -1;
public Drag() {}
@Override
public boolean onDrag(View v, DragEvent event)
{
final int action = event.getAction();
boolean state = true;
View root = v.getRootView();
ListView List = (ListView) root.findViewById(android.R.id.list);
Object holder = v.getTag() instanceof OrderHolder? (OrderHolder)v.getTag() : v.getTag();
String BookId = holder instanceof OrderHolder? String.valueOf(((OrderHolder)holder).id) : (String)holder;
switch(action)
{
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
if(PositionOfDraggedItem != -1)
{
if(this.PositionOfItemDraggedOver != PositionOfDraggedItem)
{
if(v.getBackground() != null)
v.setBackgroundDrawable(v.getContext().getResources().getDrawable(drawable.row_ovr));
}
}
break;
case DragEvent.ACTION_DRAG_LOCATION:
/**
* Set the item being
* here, as it will not
* provide location data
* anywhere else
*/
if(PositionOfDraggedItem == -1)
PositionOfDraggedItem = List.getPositionForView(v);
/**
* Set the view thats
* being dragged over
*/
this.PositionOfItemDraggedOver = List.getPositionForView(v);
int PositionOfLastVisibleView = List.getLastVisiblePosition();
int PositionOfFirstVisibleView = List.getFirstVisiblePosition();
if(this.PositionOfItemDraggedOver != -1 && (this.PositionOfItemDraggedOver != PositionOfDraggedItem)){
if(this.PositionOfItemDraggedOver == PositionOfLastVisibleView-1 || this.PositionOfItemDraggedOver == PositionOfLastVisibleView || this.PositionOfItemDraggedOver == PositionOfLastVisibleView+1){
List.smoothScrollToPosition(PositionOfLastVisibleView+1);
}else if(this.PositionOfItemDraggedOver == PositionOfFirstVisibleView-1 || this.PositionOfItemDraggedOver == PositionOfFirstVisibleView+1 || this.PositionOfItemDraggedOver == PositionOfFirstVisibleView){
List.smoothScrollToPosition(PositionOfFirstVisibleView-1);
}
}
break;
case DragEvent.ACTION_DRAG_EXITED:
if(v.getBackground() != null)
v.setBackgroundDrawable(v.getContext().getResources().getDrawable(drawable.row));
break;
case DragEvent.ACTION_DROP:
if(event != null)
{
ClipData data = event.getClipData();
if(data != null && data.getItemCount() > 0)
{
if(!data.getItemAt(0).getText().toString().equalsIgnoreCase(BookId))
{
if(v.getBackground() != null)
v.setBackgroundDrawable(v.getContext().getResources().getDrawable(drawable.row));
/**
* Ordering info for
* book being dragged
*/
String oShelfOrder = BookManager.item(v.getContext().getContentResolver(), dbhelper.BOOKS_SHELFORDER,
booksprovider.GetUri(dbhelper.BOOKS), Long.parseLong(data.getItemAt(0).getText().toString()));
String oBookOrder = BookManager.item(v.getContext().getContentResolver(), dbhelper.BOOKS_BOOKSORDER,
booksprovider.GetUri(dbhelper.BOOKS), Long.parseLong(data.getItemAt(0).getText().toString()));
/**
* Ordering info
* for book being dragged
* to
*/
String dShelfOrder = BookManager.item(v.getContext().getContentResolver(), dbhelper.BOOKS_SHELFORDER,
booksprovider.GetUri(dbhelper.BOOKS), Long.parseLong(BookId));
String dBookOrder = BookManager.item(v.getContext().getContentResolver(), dbhelper.BOOKS_BOOKSORDER,
booksprovider.GetUri(dbhelper.BOOKS), Long.parseLong(BookId));
/**
* Update book being dragged
*/
ContentValues args = new ContentValues();
args.put(dbhelper.BOOKS_SHELFORDER, dShelfOrder);
args.put(dbhelper.BOOKS_BOOKSORDER, dBookOrder);
BookManager.updateBook(v.getContext().getContentResolver(), data.getItemAt(0).getText().toString(), args);
/**
* Update book being dragged to
*/
ContentValues values = new ContentValues();
values.put(dbhelper.BOOKS_SHELFORDER, oShelfOrder);
values.put(dbhelper.BOOKS_BOOKSORDER, oBookOrder);
BookManager.updateBook(v.getContext().getContentResolver(), BookId, values);
if(v.getContext() instanceof OrderBooks){
((OrderBooks)v.getContext()).adapter.refresh();
((OrderBooks)v.getContext()).adapter.notifyDataSetChanged();
PositionOfDraggedItem = -1;
}
}
}
}
break;
case DragEvent.ACTION_DRAG_ENDED:
if(event != null && event.getResult())
{
v.invalidate();
}
break;
}
return state;
}
}
有沒有在一段時間的工作就可以了,更新,以反映最新的變化。
哇,非常感謝你!我們決定推遲此功能,因爲我們無法實現此功能。我會看看這個,看看這是否解決了我的問題。我看到你的博客,它的真實需要一週時間,因爲當我滾到兩天,我不得不放棄:) – dropsOfJupiter 2011-04-18 01:51:47
偉大的組件!對我來說工作得很好...... THX – kyp 2011-11-16 15:20:50
上面的博客帖子鏈接不起作用,但你可以在這裏找到剩下的所需:http://codegarage.no-ip.co.uk/snippets/94 – mpemburn 2014-02-17 18:58:40