根据gridview的子项点击添加一个button在一个linearlayout中,并且这个button可以移动,但是每次添加之前添加的view的位置就会重置,设置layoutParams的margin又会出现不停的闪的情况
composeMainHoriView.setOnItemClickListener(new MyHorizontalScrollView.OnItemClickListener() {
@Override
public void onClick(View view, int pos) {
final Button button = new Button(context);
button.setBackgroundResource(drableIdList.get(pos));
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(llp);
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int firstX = 0;
int firstY = 0;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
firstX = (int) event.getX();
firstY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
int currentX = (int) event.getX() - button.getWidth() / 2;
int currentY = (int) event.getY() - button.getHeight() / 2;
int x = currentX - firstX;
int y = currentY - firstY;
button.offsetTopAndBottom(y);
button.offsetLeftAndRight(x);
break;
default:
break;
}
return true;
}
});
composeMainCenterFl.addView(button);
光阴似箭催人老,日月如移越少年。