0
DragEvent.getLocalState()를 사용하여 DragAndDrop 동안 뷰의 paren (LinearLayout)에 액세스하는 방법?
위 그림에서 보았 듯이 3 개의 LinearLayouts가 있으며 각 요소에는 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");
}
}}