我试图弄清楚如何执行以下操作:
我有一个活动和一个片段作为一种导航抽屉(右侧)。 在抽屉片段里面我有一个网格视图。 目前,我可以长按该网格视图中的一个单元格,这将启动startDrag()方法。这将创建一个我可以移动的拖动阴影对象。全部根据API。
调用startDrag后,我使用接口调用托管此片段的活动,并要求它关闭抽屉。 因此,我离开了活动,并在我的手指下拖动了当前拖动的视图。 我的问题是,掉线听众。我尝试将DragListener附加到活动的根视图,但不会调用任何操作。
我错过了什么吗?这是我使用的代码:
这是抽屉片段:
mBlocksGridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// Get the block's id from adapter
mBlockId = ((BlockItem) parent.getItemAtPosition(position)).getBlockId();
// Get the clicked view
View clickedView = parent.getChildAt(position);
// Pass blockId in clipdata
ClipData clipData = ClipData.newPlainText("blockID", String.valueOf(mBlockId));
// Setup the drag shadow (the bitmap representation of the selected view)
View.DragShadowBuilder dragShadow = new View.DragShadowBuilder(clickedView);
// Start the drag mechanism
clickedView.startDrag(clipData, dragShadow, clickedView, 0);
mOnItemSingleClickListener.onBlockItemClickedEvent();
return true;
}
});
ClickListener中的最后一次调用,mOnItemSingleClickListener.onBlockItemClickedEvent();调用活动。这是在那里运行的代码:
@Override
public void onBlockItemClickedEvent() {
// check if this is a tablet or not
if (!_isTablet){
// it's not, so close the palette
_twoPaneLayout.hideRight();
}
setUpOnDragListener();
}
这是setUpOnDragListener():
private void setUpOnDragListener() {
findViewById(R.id.activity_drawing_viewer_root).setOnDragListener(new MyDragEventListener());
}
R.id.activity_drawering_viewer_root是我活动中根视图的ID。
这是MyDragEventListener类......非常简单:
public class MyDragEventListener implements View.OnDragListener {
// This is the method that the system calls when it dispatches a drag event to the
// listener.
public boolean onDrag(View v, DragEvent event) {
// Defines a variable to store the action type for the incoming event
final int action = event.getAction();
// Handles each of the expected events
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d("LOGGY5", "DragStarted");
return true;
// Im inside the original view's bounds
case DragEvent.ACTION_DRAG_ENTERED:
Log.d("LOGGY5", "We're inside original views bounds");
return true;
case DragEvent.ACTION_DRAG_LOCATION:
// Ignore the event
return true;
case DragEvent.ACTION_DRAG_EXITED:
Log.d("LOGGY5", "We existed original views bounds");
return true;
case DragEvent.ACTION_DROP:
Log.d("LOGGY5", "We dropped the shadow view" + event.getX() + ":::" + event.getY());
return true;
case DragEvent.ACTION_DRAG_ENDED:
Log.d("LOGGY5", "Drag ended" + event.getX() + ":::" + event.getY());
return true;
// An unknown action type was received.
default:
Log.e("DragDrop Example", "Unknown action type received by OnDragListener.");
break;
}
return false;
}
}
问题是Drag Listener永远不会被调用...我必须在活动中获得拖动阴影的X和Y坐标,所以一旦它被删除,我可以调用另一个方法将拖动的项放在布局中使用正确的X,Y坐标......
谢谢!
答案 0 :(得分:1)