ドラッグアンドドロップ機能についてはこのチュートリアルに従いましたが、奇妙なバグを除いて機能します。
textviewAをTextviewBにドラッグすると、次のようになります。
- textviewAを非表示
- textviewAからtextviewBにタグを渡します
- 渡されたデータをtextviewBに再表示します
textview AからBへのデータの受け渡しは100%の確率で機能しますが、奇妙なことに、textview Aは100%の確率で非表示になりません。テキストビューBなどに物理的にドラッグしている方法かどうかはわかりません。どうすればそれをより安定させることができるか考えていますか?
public boolean onDrag(View v, DragEvent event) {
//handle drag events
switch (event.getAction()) {
case DragEvent.ACTION_DROP:
//handle the dragged view being dropped over a drop view
View view = (View) event.getLocalState();
//stop displaying the view where it was before it was dragged
view.setVisibility(View.INVISIBLE);
//view dragged item is being dropped on
TextView dropTarget = (TextView) v;
//view being dragged and dropped
TextView dropped = (TextView) view;
//update the text in the target view to reflect the data being dropped
dropTarget.setText(dropped.getText());
//make it bold to highlight the fact that an item has been dropped
dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
//if an item has already been dropped here, there will be a tag
Object tag = dropTarget.getTag();
//if there is already an item here, set it back visible in its original place
if(tag!=null)
{
//the tag is the view id already dropped here
int existingID = (Integer)tag;
//set the original view visible again
findViewById(existingID).setVisibility(View.VISIBLE);
}
//set the tag in the target view to the ID of the view being dropped
dropTarget.setTag(dropped.getId());
// check choice
checkChoice(dropped,dropTarget);
break;