1
我有一個網格佈局與文本,我可以執行拖放。一切工作正常,但我想記錄下一些拖動操作完成後的網格佈局或文本的順序。我試圖在拖動執行後獲取文本,但沒有給我更新的位置。如何在拖放操作完成後在網格佈局中的文本的順序完成android
這是我的代碼,其中我生成「n」的動態文本視圖數目並將其添加到網格佈局
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;
}
}
我測試通過檢索代碼文本視圖的文本,但它在執行拖放操作之前給了我相同的文本順序。我不知道如何在網格佈局中獲取當前文本視圖的順序。
任何幫助將是偉大的!由於