我跟着this tutorial进行了拖放功能,除了一个奇怪的bug之外它还能正常工作。
将textview A拖到Textview B上假设为:
将数据从textview A传递到B可以100%的时间工作,但奇怪的是,textview A在100%的时间内都不会隐藏。我不知道它是否是我将它拖过textview 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;
答案 0 :(得分:0)
认为我发现了问题
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); COMMENT THIS OUT
}