如何在android中完成拖放操作后获得网格布局中文本的顺序
我有一个网格布局和文本,我可以执行拖放。一切正常,但我想在完成一些拖放操作后记录网格布局或文本的顺序。我尝试在执行拖动后获取文本,但没有给我更新的位置 这是我的代码,我在其中动态生成“n”个文本视图,并将其添加到网格布局如何在android中完成拖放操作后获得网格布局中文本的顺序,android,drag-and-drop,textview,Android,Drag And Drop,Textview,我有一个网格布局和文本,我可以执行拖放。一切正常,但我想在完成一些拖放操作后记录网格布局或文本的顺序。我尝试在执行拖动后获取文本,但没有给我更新的位置 这是我的代码,我在其中动态生成“n”个文本视图,并将其添加到网格布局 text = new TextView[sizeofgrid]; for (int i = 0; i < sizeofgrid; i++) { text[i] = new TextView(MainActivity.this); // cr
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;
}
});
text=newtextview[sizeofgrid];
对于(int i=0;i
下面是执行拖动的代码
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;
}
}
类DragListener实现View.OnDragListener{
@凌驾
public boolean onDrag(View v,DragEvent event){//我们正在拖动的视图和触发的事件
最终视图=(视图)事件。getLocalState();
开关(event.getAction()){
case DragEvent.ACTION\u DRAG\u LOCATION://如果视图的阴影长时间处于边界内,它将被触发
//如果悬停在自己的位置上方,则不执行任何操作
dragflag=true;
if(view==v)返回true;//可以是thr,也不能是thr..不进行区分
//获取新的列表索引
final int index=calculateNewIndex(event.getX(),event.getY());//一旦我们开始从当前位置移动,它就会开始
//正在计算新值并将重新定位
//从旧位置删除视图
gl.removeView(视图);//从原始位置删除视图
//并向新的方向推进
gl.addView(视图,索引);//将视图添加到新的cal pos
打破
案例DrageEvent.ACTION_DROP:
view.setVisibility(view.VISIBLE);
//通过在删除后检索textview的文本进行测试,但尚未更新。
对于(int i=0;i任何帮助都会很好!!谢谢我已经通过以下代码解决了问题
for (int k = 0; k < sizeofgrid; k++) {
View view = gl.getChildAt(k);
Log.d(tag,"droppedis");
// Do something with v.
if (view instanceof TextView) {
TextView editText = (TextView) view;
arr.add(editText.getText().toString());
Log.d(tag, "thelistis" + arr);
}
}
for(int k=0;k
我从父视图获取所有视图,并检查它是否是textview的实例。如果是,则我从textview获取文本并存储在列表中。我已通过以下代码解析
for (int k = 0; k < sizeofgrid; k++) {
View view = gl.getChildAt(k);
Log.d(tag,"droppedis");
// Do something with v.
if (view instanceof TextView) {
TextView editText = (TextView) view;
arr.add(editText.getText().toString());
Log.d(tag, "thelistis" + arr);
}
}
for(int k=0;k
我从父视图获取所有视图,并检查它是否是textview的实例。如果是,则我从textview获取文本并存储在列表中