dhk*_*hke 9 android scroll drag-and-drop android-listview
精简版:
DragEvent已经运行的拖放操作?还有如何注册的dragEvent,而里面已经之一,有它在当前的dragEvent听?,但我真的很喜欢更清洁的解决方案.
为了获得"正确",建议的GONE-> VISIBLE变通方法非常复杂,因为您需要确保仅在列表项可见时使用它而不是无条件地在所有当前列表视图项上使用它.在这种情况下,hack略有泄漏,没有更多的解决方法代码来实现它.
长版:
我有一个ListView.ListView自定义View 的元素包含可拖动符号(小方框),例如类似于:

可以将ListView诸如排序元素之类的项之间的小方框拖动到框中.列表项上的拖动处理程序或多或少是微不足道的:
@Override
public boolean onDragEvent(DragEvent event)
{
if ((event.getLocalState() instanceof DragableSymbolView)) {
final DragableSymbolView draggedView = (DragableSymbolView) event.getLocalState();
if (draggedView.getTag() instanceof SymbolData) {
final SymbolData symbol = (SymbolData) draggedView.getTag();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
return true;
case DragEvent.ACTION_DRAG_ENTERED:
setSelected(true);
return true;
case DragEvent.ACTION_DRAG_ENDED:
case DragEvent.ACTION_DRAG_EXITED:
setSelected(false);
return true;
case DragEvent.ACTION_DROP:
setSelected(false);
// [...] remove symbol from soruce box and add to current box
requestFocus();
break;
}
}
}
return super.onDragEvent(event);
}
Run Code Online (Sandbox Code Playgroud)
将指针放在符号上并开始拖动(即将其移动到小阈值以外)时开始拖动.
但是,现在屏幕尺寸可能不足以包含所有框,因此ListView需要滚动.我发现我需要自己实现滚动的困难方式,因为ListView在拖动时不会自动滚动.
来了ListViewScrollingDragListener:
public class ListViewScrollingDragListener
implements View.OnDragListener {
private final ListView _listView;
public static final int DEFAULT_SCROLL_BUFFER_DIP = 96;
public static final int DEFAULT_SCROLL_DELTA_UP_DIP = 48;
public static final int DEFAULT_SCROLL_DELTA_DOWN_DIP = 48;
private int _scrollDeltaUp;
private int _scrollDeltaDown;
private boolean _doScroll = false;
private boolean _scrollActive = false;
private int _scrollDelta = 0;
private int _scrollDelay = 250;
private int _scrollInterval = 100;
private int _scrollBuffer;
private final Rect _visibleRect = new Rect();
private final Runnable _scrollHandler = new Runnable() {
@Override
public void run()
{
if (_doScroll && (_scrollDelta != 0) && _listView.canScrollVertically(_scrollDelta)) {
_scrollActive = true;
_listView.smoothScrollBy(_scrollDelta, _scrollInterval);
_listView.postDelayed(this, _scrollInterval);
} else {
_scrollActive = false;
}
}
};
public ListViewScrollingDragListener(final ListView listView, final boolean attach)
{
_scrollBuffer = UnitUtil.dipToPixels(listView, DEFAULT_SCROLL_BUFFER_DIP);
_scrollDeltaUp = -UnitUtil.dipToPixels(listView, DEFAULT_SCROLL_DELTA_UP_DIP);
_scrollDeltaDown = UnitUtil.dipToPixels(listView, DEFAULT_SCROLL_DELTA_DOWN_DIP);
_listView = listView;
if (attach) {
_listView.setOnDragListener(this);
}
}
public ListViewScrollingDragListener(final ListView listView)
{
this(listView, true);
}
protected void handleDragLocation(final float x, final float y)
{
_listView.getGlobalVisibleRect(_visibleRect);
if (_visibleRect.contains((int) x, (int) y)) {
if (y < _visibleRect.top + _scrollBuffer) {
_scrollDelta = _scrollDeltaUp;
_doScroll = true;
} else if (y > _visibleRect.bottom - _scrollBuffer) {
_scrollDelta = _scrollDeltaDown;
_doScroll = true;
} else {
_doScroll = false;
_scrollDelta = 0;
}
if ((_doScroll) && (!_scrollActive)) {
_scrollActive = true;
_listView.postDelayed(_scrollHandler, _scrollDelay);
}
}
}
public ListView getListView()
{
return _listView;
}
@Override
public boolean onDrag(View v, DragEvent event)
{
/* hide sequence controls during drag */
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_ENTERED:
_doScroll = true;
break;
case DragEvent.ACTION_DRAG_EXITED:
case DragEvent.ACTION_DRAG_ENDED:
case DragEvent.ACTION_DROP:
_doScroll = false;
break;
case DragEvent.ACTION_DRAG_LOCATION:
handleDragLocation(event.getX(), event.getY());
break;
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
ListView当您在其可见区域的上边界或下边界附近拖动时,这基本上会滚动.它并不完美,但它足够好.
然而,有一个问题:
当列表滚动到先前不可见的元素时,该元素不会接收DragEvents.在其上拖动符号时,它不会被选中(突出显示),也不会接受掉落.
关于如何使"滚动"视图的任何想法都DragEvent从已经有效的拖放操作中获得?
所以根本问题是ViewGroup(ListView扩展)缓存要通知的子级列表DragEvent。此外,它仅在接收ACTION_DRAG_STARTED时填充此缓存。有关更多详细信息,请仔细阅读此处的源代码。
继续解决问题!我们不会监听 的各个行上的放置事件ListView,而是监听它们本身ListView。然后,根据事件的坐标,我们将确定拖动的视图正在从哪一行拖动到哪一行或将鼠标悬停在哪一行上。当删除发生时,我们将执行从前一行中删除并添加到新行的事务。
private void init(Context context) {
setAdapter(new RandomIconAdapter()); // Adapter that contains our data set
setOnDragListener(new ListDragListener());
mListViewScrollingDragListener = new ListViewScrollingDragListener(this, false);
}
ListViewScrollingDragListener mListViewScrollingDragListener;
private class ListDragListener implements OnDragListener {
// The view that our dragged view would be dropped on
private View mCurrentDropZoneView = null;
private int mDropStartRowIndex = -1;
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_LOCATION:
// Update the active drop zone based on the position of the event
updateCurrentDropZoneView(event);
// Funnel drag events to separate listener to handle scrolling near edges
mListViewScrollingDragListener.onDrag(v, event);
if( mDropStartRowIndex == -1 ) // Only initialize once per drag->drop gesture
{
mDropStartRowIndex = indexOfChild(mCurrentDropZoneView) + getFirstVisiblePosition();
log("mDropStartRowIndex %d", mDropStartRowIndex);
}
break;
case DragEvent.ACTION_DRAG_ENDED:
case DragEvent.ACTION_DRAG_EXITED:
mCurrentDropZoneView = null;
mDropStartRowIndex = -1;
break;
case DragEvent.ACTION_DROP:
// Update our data set based on the row that the dragged view was dropped in
int finalDropRow = indexOfChild(mCurrentDropZoneView) + getFirstVisiblePosition();
updateDataSetWithDrop(mDropStartRowIndex, finalDropRow);
// Let adapter update ui
((BaseAdapter)getAdapter()).notifyDataSetChanged();
break;
}
// The ListView handles ALL drag events all the time. Fine for now since we don't need to
// drag -> drop outside of the ListView.
return true;
}
private void updateDataSetWithDrop(int fromRow, int toRow) {
log("updateDataSetWithDrop fromRow %d and toRow %d", fromRow, toRow);
sIconsForListItems[fromRow]--;
sIconsForListItems[toRow]++;
}
// NOTE: The DragEvent in local to DragDropListView, as are children coordinates
private void updateCurrentDropZoneView(DragEvent event) {
View previousDropZoneView = mCurrentDropZoneView;
mCurrentDropZoneView = findFrontmostDroppableChildAt(event.getX(), event.getY());
log("mCurrentDropZoneView updated to %d for x/y : %f/%f with action %d",
mCurrentDropZoneView == null ? -1 : indexOfChild(mCurrentDropZoneView) + getFirstVisiblePosition(),
event.getX(), event.getY(), event.getAction());
if (mCurrentDropZoneView != previousDropZoneView) {
if (previousDropZoneView != null) previousDropZoneView.setSelected(false);
if (mCurrentDropZoneView != null) mCurrentDropZoneView.setSelected(true);
}
}
}
/**
* The next four methods are utility methods taken from Android Source Code. Most are package-private on View
* or ViewGroup so I'm forced to replicate them here. Original source can be found:
* http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.0_r1/android/view/ViewGroup.java#ViewGroup.findFrontmostDroppableChildAt%28float%2Cfloat%2Candroid.graphics.PointF%29
*/
private View findFrontmostDroppableChildAt(float x, float y) {
int childCount = this.getChildCount();
for(int i=0; i<childCount; i++)
{
View child = getChildAt(i);
if (isTransformedTouchPointInView(x, y, child)) {
return child;
}
}
return null;
}
static public boolean isTransformedTouchPointInView(float x, float y, View child) {
PointF point = new PointF(x, y);
transformPointToViewLocal(point, child);
return pointInView(child, point.x, point.y);
}
static public void transformPointToViewLocal(PointF pointToModify, View child) {
pointToModify.x -= child.getLeft();
pointToModify.y -= child.getTop();
}
static public boolean pointInView(View v, float localX, float localY) {
return localX >= 0 && localX < (v.getRight() - v.getLeft())
&& localY >= 0 && localY < (v.getBottom() - v.getTop());
}
static final int[] sIconsForListItems;
static final int NUM_LIST_ITEMS = 50;
static final int MAX_NUM_ICON_PER_ELEMENT = 8;
static {
sIconsForListItems = new int[NUM_LIST_ITEMS];
for (int i=0; i < NUM_LIST_ITEMS; i++)
{
sIconsForListItems[i] = (getRand(MAX_NUM_ICON_PER_ELEMENT));
}
}
private static final String TAG = DragDropListView.class.getSimpleName();
private static void log(String format, Object... args) {
Log.d(TAG, String.format(format, args));
}
Run Code Online (Sandbox Code Playgroud)
有很多评论,所以希望代码是自我记录的。一些注意事项:
| 归档时间: |
|
| 查看次数: |
819 次 |
| 最近记录: |