【发布时间】:2016-09-19 07:05:16
【问题描述】:
我有一个带有文本的网格布局,我可以在其中执行拖放操作。一切正常,但我想在完成一些 拖放操作 后记录网格布局或文本的顺序。我尝试在执行拖动后获取文本,但它没有给我更新的位置。
这是我的代码,我在其中动态生成“n”个文本视图并将其添加到 GRID LAYOUT
text = new TextView[sizeofgrid];
for (int i = 0; i < sizeofgrid; i++) {
text[i] = new TextView(MainActivity.this); // create the txt view
first = new GridLayout.LayoutParams();
first.width = pergrid;
text[i].setLayoutParams(first);
text[i].setGravity(Gravity.CENTER);
first.height = LinearLayout.LayoutParams.MATCH_PARENT;
text[i].setText(String.valueOf(finallist.get(i)));
text[i].setTextSize(20);
text[i].setBackgroundResource(R.drawable.back);
text[i].setTextColor((Color.parseColor("#0B0B0B")));
// text[i].setPadding(5, 25, 10, 25);
text[i].setPadding(5, 10, 0, 25);
first.setMargins(0, 10, 5, 20);// margin can be set nly to layout and cant be set directly to textview
text[i].setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
final ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.VISIBLE);
Log.d(tag, "calleddd");
return true;
}
});
这是执行拖动的代码
class DragListener implements View.OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) { // the view which we are dragging and the events whcih gets triggred
final View view = (View) event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_LOCATION: // if the shadow of the view is within the bounds for a long amt of time , it will get triggred
// do nothing if hovering above own position
dragflag = true;
if (view == v) return true; // can or cant be thr..makes no diff
// get the new list index
final int index = calculateNewIndex(event.getX(), event.getY()); // as soon as we start moving from current pos, it will start
//calculating new value and will get relocated
// remove the view from the old position
gl.removeView(view); //remove the view from original pos
// and push to the new
gl.addView(view, index); // add the view to newly cal pos
break;
case DragEvent.ACTION_DROP:
view.setVisibility(View.VISIBLE);
//tested by retrieving the text of textview after dropping but it hasn't got updated.
for(int i = 0; i < sizeofgrid; i++)
{
Log.d(tag,"thetexssis"+text[i].getText().toString());
}
break;
case DragEvent.ACTION_DRAG_ENDED:
if (!event.getResult()) {
view.setVisibility(View.VISIBLE);
}
break;
}
return true;
}
}
我通过检索文本视图的文本进行了测试,但它在执行拖放之前给了我相同的文本顺序。我不知道如何获取当前文本视图在网格布局中的顺序。
任何帮助都会很棒!谢谢
【问题讨论】:
标签: android drag-and-drop textview