我正在使用相对布局。我需要在移动图像时更改背景图像,移动到特定位置。但是当布局移动(拖动)时,我无法访问移动视图和更改背景。
感谢。
class MyDragListener implements OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DRAG_LOCATION:
//come here when moving view
break;
case DragEvent.ACTION_DROP:
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
RelativeLayout container = (RelativeLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
答案 0 :(得分:0)
您可以使用以下代码
case DragEvent.ACTION_DRAG_STARTED:
((RelativeLayout) v).setBackgroundColor(Color.RED);
break;
和ACTION_DROP如果需要,您可以再次更改。