android滚动,而拖放

您好我正在做一个ListView的拖放。 它工作很好。 但是我失去了点击和滚动的能力。 如何保留这两个? 我一直在想,这个拖放应该发生在长时间按下该项目,而不是一个正常的新闻。 这是我的onTouch位于扩展ListView的MyListView

@Override public boolean onTouchEvent(MotionEvent ev) { final int action = ev.getAction(); final int x = (int) ev.getX(); final int y = (int) ev.getY(); if (action == MotionEvent.ACTION_DOWN && x < this.getWidth()/4) { this._dragMode = true; } if (!this._isDragNDrop) return super.onTouchEvent(ev); switch (action) { case MotionEvent.ACTION_DOWN: this._startPosition = pointToPosition(x,y); if (this._startPosition != INVALID_POSITION) { int mItemPosition = this._startPosition - getFirstVisiblePosition(); this._dragPointOffset = y - getChildAt(mItemPosition).getTop(); this._dragPointOffset -= ((int)ev.getRawY()) - y; startDrag(mItemPosition,y); drag(x,y); } break; case MotionEvent.ACTION_MOVE: drag(x,y); break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: default: this._dragMode = false; //_isDragNDrop = false; this._endPosition = pointToPosition(x,y); stopDrag(this._startPosition - getFirstVisiblePosition()); if (this._dropListener != null && this._startPosition != INVALID_POSITION && this._endPosition != INVALID_POSITION) this._dropListener.onDrop(this._startPosition, this._endPosition); break; } return true; } 

Solutions Collecting From Web of "android滚动,而拖放"

我只是在我的应用程序中实现了拖放列表视图,拖动通过长按一个项目开始。

源代码在GitHub上: DraggableListView ,我有一个博客文章来描述它。 希望它可以帮助你。

我正在实现类似的东西。 希望这会帮助别人,因为我仍然在纠结。

 ... 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; } 

}

有一段时间没有工作,更新以反映最新的变化。

看看这个问题的额外帮助。 我也有一个名为DragSortListView的组件,我花了很长时间来完善。 你可以在这里find它:

我可以帮你整合长时间发起的拖动在我以上的回购叉。

我实施了这个解决scheme,为我工作。 以下方法在我的Activity中,当dragEvent是ACTION_DRAG_ENTERED时,在我的dragEventListener中被调用。 我声明的一些属性:

 //first visible item (getFirstVisiblePosition() does not work for me) int firstVisible; //number of scrolls int nbrScroll; //my listview to scroll ListView mListView; //the adapter of my listview SimpleAdapter mAdapter; 

 public void scrollDrag(int position) { //this is the position of the item in my listview //to scroll down //test if the item entered is the last visible if (position == mListView.getLastVisiblePosition() && id < mListView.getCount()) { mAdapter.notifyDataSetChanged(); mRightDrawerList.post(new Runnable() { @Override public void run() { //scroll down mListView.smoothScrollToPosition(mListView .getLastVisiblePosition() + 1); firstVisible++; } }); //to scroll up //test if the item entered is the first visible } else if (position == firstVisible && position !=0) { mAdapter.notifyDataSetChanged(); mListView.post(new Runnable() { @Override public void run() { //scroll up mListView.smoothScrollToPosition(firstVisible-1); firstVisible --; } }); } } 

希望它能帮助别人。

他们,我已经在rickey的代码中做了一些改变,这对我很有用。

 if (PositionOfItemDraggedOver == PositionOfLastVisibleView) order_item_list_lv.smoothScrollToPosition(PositionOfLastVisibleView + 1); if (PositionOfItemDraggedOver == PositionOfFirstVisibleView) order_item_list_lv.smoothScrollToPosition(PositionOfFirstVisibleView - 1);