我按照本教程进行拖放功能,除了一个奇怪的错误外,它可以工作。
将 textview A 拖到 Textview B 上是假设:
- 隐藏文本视图 A
- 将标签从 textview A 传递到 textview B
- 在 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;