gpt4 book ai didi

android - 拖放 API - OnDragListener 问题

转载 作者:行者123 更新时间:2023-11-29 01:39:44 28 4
gpt4 key购买 nike

我正在尝试弄清楚如何执行以下操作:

我有一个 Activity 和一个 fragment 充当一种抽屉导航(右侧)。在那个抽屉 fragment 中,我有一个 GridView 。目前,我可以长按该 GridView 中的一个单元格,这将启动 startDrag() 方法。这将创建一个我可以四处移动的拖动阴影对象。全部根据 API。

调用 startDrag 后,我使用接口(interface)调用托管此 fragment 的 Activity ,并要求它关闭抽屉。所以我只剩下 Activity 和当前拖动的 View 在我的手指下。我的问题是,drop listener。我尝试将 DragListener 附加到 Activity 的 Root View ,但没有调用任何操作。

我错过了什么吗?这是我使用的代码:

抽屉 fragment :

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();调用 Activity 。这是在那里运行的代码:

@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 是我 Activity 中 Root View 的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 永远不会被调用...我必须在 Activity 中获取拖动阴影的 X 和 Y 坐标,所以一旦它被放下,我就可以调用另一个方法来将拖动的项目放置在布局中使用正确的 X、Y 坐标...

谢谢!

最佳答案

好吧,我发现了问题……DragListener 返回了 false,它有效地将事件传递到链中并且自己没有使用它。

将其更改为 true 并且监听器变得活跃。

第一段here .解释一下。

关于android - 拖放 API - OnDragListener 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25505498/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com