我的代碼迄今其次,因爲我測試一下:當你在一個TableLayout的子項中時,你可以拖放一個TextView嗎?
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的一部分,它可能會導致這種情況。有任何想法嗎?