addView時にドラッグしたViewがリセットされてしまう
動的に追加したviewをドラッグ出来るようにしているのですが、
View.layoutで設定した座標が、layoutにaddViewした時点でリセットされてしまいます。
コレを回避する方法は無いでしょうか?
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public Activity mActivity;
public RelativeLayout layout;
public PlaceholderFragment() {}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
layout = (RelativeLayout) rootView.findViewById(R.id.layout);
ImageView dragView = (ImageView) rootView.findViewById(R.id.imageView1);
DragViewListener listener = new DragViewListener(dragView);
dragView.setOnTouchListener(listener);
return rootView;
}
public void addImageView(){
ImageView v = new ImageView(mActivity);
//v.setL
layout.addView(v, new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
v.setImageDrawable( mActivity.getResources().getDrawable(R.drawable.ueki));
DragViewListener listener = new DragViewListener(v);
v.setOnTouchListener(listener);
}
//ドラッグイベント処理
public class DragViewListener implements OnTouchListener {
// ドラッグ対象のView
private ImageView dragView;
// ドラッグ中に移動量を取得するための変数
private int oldx;
private int oldy;
public DragViewListener(ImageView dragView) {
this.dragView = dragView;
}
@Override
public boolean onTouch(View view, MotionEvent event) {
int x = (int) event.getRawX();
int y = (int) event.getRawY();
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
int left = dragView.getLeft() + (x - oldx);
int top = dragView.getTop() + (y - oldy);
dragView.layout(left, top, left + dragView.getWidth(), top
+ dragView.getHeight());
break;
}
oldx = x;
oldy = y;
return true;
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(getArguments().getInt(
ARG_SECTION_NUMBER));
mActivity = activity;
}
}