我想创建一个具有拖放功能的可扩展列表视图,即我可以通过拖放来排列内部或外部项目
问问题
2068 次
4 回答
1
您从实现 View.OnDragListener 的类创建拖动事件侦听器对象(“侦听器”)。您使用 View 对象的 setOnDragListener() 方法为 View 设置拖动事件侦听器对象。每个 View 对象也有一个 onDragEvent() 回调方法。这两个都在 开发人员部分中进行了更详细的描述。
请参阅此链接以获取工作示例:
bit.ly/16r49T3
于 2013-08-16T05:30:57.583 回答
1
您需要添加一些侦听器才能使其正常工作。这些监听器我放置在它们出现的片段的 OnStart 中。
第一个侦听器如下,假设您将列表视图称为“lvListView”(这是为了拖动孩子,我在这里的其他地方找到了它):
lvListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPosition = ExpandableListView.getPackedPositionGroup(id);
int childPosition = ExpandableListView.getPackedPositionChild(id);
// You now have everything that you would as if this was an OnChildClickListener()
// Add your logic here.
//include this:
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, shadowBuilder, lvItems.getItemAtPosition(position), 0);
// Return true as we are handling the event.
return true;
}
return false;
}
});
这允许你拖动孩子,但现在你需要一个监听器来监听你所有的 DubSteppin'(Drop)。我在代码中紧随其后放置了以下内容:
lvListView.setOnDragListener(new AdapterView.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
//layoutParams = (RelativeLayout.LayoutParams)v.getLayoutParams();
Log.i("msg", "Action is DragEvent.ACTION_DRAG_STARTED");
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.i("msg", "Action is DragEvent.ACTION_DRAG_ENTERED");
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.i("msg", "Action is DragEvent.ACTION_DRAG_EXITED");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_LOCATION:
Log.i("msg", "Action is DragEvent.ACTION_DRAG_LOCATION");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
Log.i("msg", Integer.toString(x_cord) + "," + Integer.toString(y_cord));
int nPointToPosition = lvListView.pointToPosition(x_cord,y_cord);
if(lvListView.getItemAtPosition(nCheck)!= null) {
// THE FUN PART IS HERE!
// ******this is the header list number******
int ngroupPosition = lvListView.getPackedPositionGroup(lvListView.getExpandableListPosition(nCheck));
// ******this is the child position******
int nchildPosition = lvListView.getPackedPositionChild(lvListView.getExpandableListPosition(nCheck));
}
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.i("msg", "Action is DragEvent.ACTION_DRAG_ENDED");
// This is where I added some activities....
break;
case DragEvent.ACTION_DROP:
Log.i("msg", "ACTION_DROP event");
// This is also a good place, place with it and see what you want to do
break;
default:
break;
}
//return value
return true;
}
});//end DragListener
很抱歉“necropost”,但我想把它向前支付,因为通常我在这里的代码块中找到我的答案。
我也从其他代码块中获得了大部分内容,并想出了办法,所以如果你认出你的代码,谢谢!
于 2016-02-03T14:05:25.263 回答
0
于 2013-08-16T05:28:50.233 回答
0
如果有人仍在寻找解决方案,我修改了拖放列表视图并将其更新为拖放可扩展列表视图,请在此处查看答案
于 2019-03-20T19:55:35.170 回答