到目前为止,我的代码如下所示,因为我正在对其进行测试:
public class TestActivity extends Activity {
TableLayout table;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
table = (TableLayout)findViewById(R.id.table);
setStuff();
setStuff2();
setStuff3();
}
public void setStuff() {
LayoutInflater inflater = LayoutInflater.from(this);
TextView view = (TextView)inflater.inflate(R.layout.some_text, table, false);
view.setText("First");
view.setTag(String.valueOf(table.getChildCount()));
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
v.startDrag(ClipData.newPlainText("index", v.getTag().toString()), new View.DragShadowBuilder(v), null, 0);
Toast.makeText(v.getContext(), "index you are clicking on is "+v.getTag().toString(), Toast.LENGTH_SHORT).show();
return false;
}
});
table.addView(view);
}
public void setStuff2() {
LayoutInflater inflater = LayoutInflater.from(this);
TextView view = (TextView)inflater.inflate(R.layout.some_text, table, false);
view.setText("Second");
view.setTag(String.valueOf(table.getChildCount()));
table.addView(view);
}
public void setStuff3() {
LayoutInflater inflater = LayoutInflater.from(this);
TextView view = (TextView)inflater.inflate(R.layout.some_text, table, false);
view.setText("Three");
view.setTag(String.valueOf(table.getChildCount()));
table.addView(view);
}
protected class MyDragListener implements View.OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
//Stuff to check the event action cases
return false;
}
}//end of MyDragListener
现在,当我长按第一项时,它运行正常,因为我收到了 Toast 通知。虽然我没有看到 DragShadow,但我应该会看到。我的猜测是,因为它是 TableLayout 的一部分,所以可能会导致这种情况。有任何想法吗?