如何使用 DragEvent.getLocalState() 在拖放过程中访问父视图(LinearLayout)?
How to access paren (LinearLayout) t of view during DragAndDrop using DragEvent.getLocalState()?
如上图所示,我有 3 个 LinearLayout,每个里面都有一个 ImageView 和一个 TextView。每个图像视图都附加了一个 OnLongClickListener。封装所有对象的 AbsoluteLayout 附加了一个 OnDragListener。
在 onDrag 方法中,我需要获取作为我正在拖动的 ImageView 的父级的 LinearLayout,以便我可以重新定位整个 LinearLayout 设置上边距和左边距。
我写了下面的代码希望我可以访问父 LinearLayout 但没有成功。
@Override
public boolean onDrag(View view, DragEvent event) {
if(event.getAction() == DragEvent.ACTION_DROP){
View v = (View) event.getLocalState();
ImageView iv = (ImageView) v;
ViewParent parent = iv.getParent();
LinearLayout l;
if (parent == null) {
Log.d("TEST", "this.getParent() is null");
}
else {
if (parent instanceof ViewGroup) {
ViewParent grandparent = ((ViewGroup) parent).getParent();
if (grandparent == null) {
Log.d("TEST", "((ViewGroup) this.getParent()).getParent() is null");
}
else {
if (parent instanceof AbsoluteLayout) {
l = (LinearLayout) grandparent;
Log.d("TEST","Successfully acquired linear layout");
}
else {
Log.d("TEST", "((ViewGroup) this.getParent()).getParent() is not a RelativeLayout");
}
}
}
else {
Log.d("TEST", "this.getParent() is not a ViewGroup");
}
}}
事实证明它就像执行以下操作一样简单
@Override
public boolean onDrag(View view, DragEvent event) {
String TAG = "DragDrop";
switch (event.getAction()) {
case DragEvent.ACTION_DROP: {
View v = (View) event.getLocalState();
ImageView iv = (ImageView) v;
ViewParent parent = iv.getParent();
LinearLayout l = (LinearLayout) parent;
AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams)l.getLayoutParams();
x = (int) event.getX() - (l.getWidth() / 2);
y = (int) event.getY() - (l.getHeight() / 2) + 30;
params.x = x;
params.y = y;
l.setLayoutParams(params);
iv.setVisibility(iv.VISIBLE);
//boardName.setVisibility(View.VISIBLE);
break;
}
}
return true;
}
如上图所示,我有 3 个 LinearLayout,每个里面都有一个 ImageView 和一个 TextView。每个图像视图都附加了一个 OnLongClickListener。封装所有对象的 AbsoluteLayout 附加了一个 OnDragListener。
在 onDrag 方法中,我需要获取作为我正在拖动的 ImageView 的父级的 LinearLayout,以便我可以重新定位整个 LinearLayout 设置上边距和左边距。
我写了下面的代码希望我可以访问父 LinearLayout 但没有成功。
@Override
public boolean onDrag(View view, DragEvent event) {
if(event.getAction() == DragEvent.ACTION_DROP){
View v = (View) event.getLocalState();
ImageView iv = (ImageView) v;
ViewParent parent = iv.getParent();
LinearLayout l;
if (parent == null) {
Log.d("TEST", "this.getParent() is null");
}
else {
if (parent instanceof ViewGroup) {
ViewParent grandparent = ((ViewGroup) parent).getParent();
if (grandparent == null) {
Log.d("TEST", "((ViewGroup) this.getParent()).getParent() is null");
}
else {
if (parent instanceof AbsoluteLayout) {
l = (LinearLayout) grandparent;
Log.d("TEST","Successfully acquired linear layout");
}
else {
Log.d("TEST", "((ViewGroup) this.getParent()).getParent() is not a RelativeLayout");
}
}
}
else {
Log.d("TEST", "this.getParent() is not a ViewGroup");
}
}}
事实证明它就像执行以下操作一样简单
@Override
public boolean onDrag(View view, DragEvent event) {
String TAG = "DragDrop";
switch (event.getAction()) {
case DragEvent.ACTION_DROP: {
View v = (View) event.getLocalState();
ImageView iv = (ImageView) v;
ViewParent parent = iv.getParent();
LinearLayout l = (LinearLayout) parent;
AbsoluteLayout.LayoutParams params = (AbsoluteLayout.LayoutParams)l.getLayoutParams();
x = (int) event.getX() - (l.getWidth() / 2);
y = (int) event.getY() - (l.getHeight() / 2) + 30;
params.x = x;
params.y = y;
l.setLayoutParams(params);
iv.setVisibility(iv.VISIBLE);
//boardName.setVisibility(View.VISIBLE);
break;
}
}
return true;
}