android drag and drop listview,listview - Android List View Drag and Drop sort - Stack Overflow

这篇博客分享了一个无需外部依赖的Android ListView实现拖拽排序的代码实现。通过创建自定义Adapter扩展ArrayAdapter,并在Adapter内设置OnTouchListener监听ListView项的触摸事件来开始拖动。拖动过程中,监听ListView的触摸事件来处理交换位置和刷新显示,并提供了开始拖动和停止拖动的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I recently stumbled upon this great Gist that gives a working implementation of a drag sort ListView, with no external dependencies needed.

Basically it consists on creating your custom Adapter extending ArrayAdapter as an inner class to the activity containing your ListView. On this adapter one then sets an onTouchListener to your List Items that will signal the start of the drag.

In that Gist they set the listener to a specific part of the layout of the List Item (the "handle" of the item), so one does not accidentally move it by pressing any part of it. Personally, I preferred to go with an onLongClickListener instead, but that is up to you to decide. Here an excerpt of that part:

public class MyArrayAdapter extends ArrayAdapter {

private ArrayList mStrings = new ArrayList();

private LayoutInflater mInflater;

private int mLayout;

//constructor, clear, remove, add, insert...

@Override

public View getView(final int position, View convertView, ViewGroup parent) {

ViewHolder holder;

View view = convertView;

//inflate, etc...

final String string = mStrings.get(position);

holder.title.setText(string);

// Here the listener is set specifically to the handle of the layout

holder.handle.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View view, MotionEvent motionEvent) {

if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {

startDrag(string);

return true;

}

return false;

}

});

// change color on dragging item and other things...

return view;

}

}

This also involves adding an onTouchListener to the ListView, which checks if an item is being dragged, handles the swapping and invalidation, and stops the drag state. An excerpt of that part:

mListView.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View view, MotionEvent event) {

if (!mSortable) { return false; }

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN: {

break;

}

case MotionEvent.ACTION_MOVE: {

// get positions

int position = mListView.pointToPosition((int) event.getX(),

(int) event.getY());

if (position < 0) {

break;

}

// check if it's time to swap

if (position != mPosition) {

mPosition = position;

mAdapter.remove(mDragString);

mAdapter.insert(mDragString, mPosition);

}

return true;

}

case MotionEvent.ACTION_UP:

case MotionEvent.ACTION_CANCEL:

case MotionEvent.ACTION_OUTSIDE: {

//stop drag state

stopDrag();

return true;

}

}

return false;

}

});

Finally, here is how the stopDrag and startDrag methods look like, which handle the enabling and disabling of the drag process:

public void startDrag(String string) {

mPosition = -1;

mSortable = true;

mDragString = string;

mAdapter.notifyDataSetChanged();

}

public void stopDrag() {

mPosition = -1;

mSortable = false;

mDragString = null;

mAdapter.notifyDataSetChanged();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值