【发布时间】:2016-06-26 23:32:18
【问题描述】:
我正在寻找一个方向。我有一个纸牌融合游戏(Five Kings),已经上线了大约 6 个月;我的最新版本 0.9.22 自 3 月以来一直稳定。但是,最近我收到了有关用户无法将丢弃物拖到丢弃物堆的报告,并且常见的线程似乎是 Samsung S7 with Android 6.0 。当您从手上拖出一张卡片时,您可以拖拽的地方会变成透明的,当您拖过它们时,它们会恢复正常(alpha=1)。您可以拖动的其他地方似乎工作正常,但丢弃堆没有变暗或变亮,这让我认为拖动事件侦听器不起作用。
这是DiscardPileDragEventListener:
class DiscardPileDragEventListener implements View.OnDragListener {
// This is the method that the system calls when it dispatches a drag event to the
// listener.
@Override
public boolean onDrag(final View v, final DragEvent event) {
//that we are not ready to accept the drag if not at the right point of the play
CardView cv = (CardView)v;
// 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:
// Determines if this View can accept the dragged data
if (cv.isAcceptDrag() && event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
//fades out of the draw pile
cv.startAnimation(Utilities.instantFade(FiveKings.HALF_TRANSPARENT_ALPHA, FiveKings.HALF_TRANSPARENT_ALPHA));
// returns true to indicate that the View can accept the dragged data.
return true;
} else {
//remind user what they should be doing if in the first few rounds
((FiveKings) cv.getContext()).setShowHint(null, FiveKings.HandleHint.SHOW_HINT, FiveKings.GameDifficulty.EASY);
// Returns false. During the current drag and drop operation, this View will
// not receive events again until ACTION_DRAG_ENDED is sent.
return false;
}
case DragEvent.ACTION_DRAG_ENTERED:
cv.clearAnimation();
return true;
case DragEvent.ACTION_DRAG_LOCATION:
// Ignore the event
return true;
case DragEvent.ACTION_DRAG_EXITED:
cv.startAnimation(Utilities.instantFade(FiveKings.HALF_TRANSPARENT_ALPHA, FiveKings.HALF_TRANSPARENT_ALPHA));
return true;
case DragEvent.ACTION_DROP:
cv.clearAnimation();
// Gets the item containing the dragged data
ClipData.Item item = event.getClipData().getItemAt(0); //this is the View index to recover which card
// Gets the text data from the item.
String dragData = item.getText().toString();
//Handle exception here and return false (drop wasn't handled) if it wasn't found
int iView = -1;
try {
iView = Integer.parseInt(dragData);
}catch (NumberFormatException e) {
return false;
}
return ((FiveKings)cv.getContext()).discardedCard(iView);
case DragEvent.ACTION_DRAG_ENDED:
if (cv.isAcceptDrag()) cv.clearAnimation();
return true;
// An unknown action type was received.
default:
Log.e("DiscardPileDragEventListener", "Unknown action type received by OnDragListener.");
}
return false;
}
}
这是设置实际拖动卡片的代码sn-ps;在这个 CardView 中是 ImageView 的一个子类:
for (Card card : meld) {
//highlight wildcards unless no dragging (Computer turn, or Human final turn)
CardView cv = new CardView(this, card, iView, allowDragging ? highlightWildCardRank : null);
cv.setTag(iView); //allows us to pass the index into the dragData without dealing with messy object passing
...
cv.bringToFront();
cv.setClickable(false); //TODO:B: Allow selection by clicking for multi-drag?
if (allowDragging) {//no dragging of computer cards or Human cards after final turn
cv.setOnDragListener(new CardViewDragEventListener()); //needed per-card to reset the dragged card to normal visibility (alpha)
cv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() != MotionEvent.ACTION_DOWN) return false;
// Create a new ClipData using the tag as a label, the plain text MIME type, and
// the string containing the View index. This will create a new ClipDescription object within the
// ClipData, and set its MIME type entry to "text/plain"
ClipData dragData = ClipData.newPlainText("Discard", v.getTag().toString());
View.DragShadowBuilder cardDragShadow = new CardViewDragShadowBuilder(v);
v.startAnimation(Utilities.instantFade(FiveKings.HALF_TRANSPARENT_ALPHA, FiveKings.HALF_TRANSPARENT_ALPHA));
// Starts the drag
v.startDrag(dragData, cardDragShadow, null, 0);
return true;
}//end onClick
});
}//end if allowDragging
...
在 onCreate 中,我设置了 Discard Pile onDragListener:
...
mDiscardPile.setOnDragListener(new DiscardPileDragEventListener());
...
一个问题是我在 Discard Pile 的父视图中还有一个 onDragListener,当您将卡片拖到 Discard Pile 时会提供消息。这在我的测试中运行良好,但我只是想知道它是否以某种方式相关。
如果有任何关于前进方向的建议,将不胜感激。
【问题讨论】:
标签: android drag-and-drop android-6.0-marshmallow samsung-mobile