Android 跨布局拖动控件的问题(已附源码,希望高手改善)
我的思路是:长按图片,出现PopupWindow(已达到浮在最上面跨布局效果)并在里面new一个一样图片,并
给它绑定onTocuh事件,在onTocuh事件里根据移动的坐标动态修改新图片的位置,从而实现拖动的效果。
问题在于:
PopupWindow里的新图片需要再次触屏才能激发刚才给它绑定的onTocuh事件。这样导致整过方案需要点击两次。
希望高手们改正!!!。
java源码:
package test.tocuh;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Test_TocuhActivity extends Activity {
/** Called when the activity is first created. */
RelativeLayout mLayoutGroup = null;
private PopupWindow mPop;
private Button main_btn;
private ImageView main_image;
private LinearLayout main_ll;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLayoutGroup = new RelativeLayout(this);
mLayoutGroup.setBackgroundColor(R.color.item_imgTag_bg);
main_btn = (Button) findViewById(R.id.main_btn);
main_ll = (LinearLayout) findViewById(R.id.main_ll);
main_image = (ImageView) findViewById(R.id.imageView1);
main_image.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
initPopWindow();
mLayoutGroup.removeAllViews();
ImageButton imgBtn = new ImageButton(Test_TocuhActivity.this);
imgBtn.setImageDrawable(((ImageView) v).getDrawable());
imgBtn.setX(v.getX());
imgBtn.setY(v.getY());
imgBtn.setClickable(true);
imgBtn.setOnTouchListener(touchListener);
mLayoutGroup.addView(imgBtn);
mLayoutGroup.setFocusableInTouchMode(true);
imgBtn.requestFocus();
mPop.showAtLocation(
Test_TocuhActivity.this.findViewById(R.id.main_root),
Gravity.CENTER, 0, 0);// 在屏幕居中,无偏移
return false;
}
});
main_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
main_ll.removeAllViews();
}
});
}
OnTouchListener touchListener = new OnTouchListener() {
int temp[] = new int[] { 0, 0 };
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
int eventAction = arg1.getAction();
int x = (int) arg1.getRawX();
int y = (int) arg1.getRawY();
switch (eventAction) {
case MotionEvent.ACTION_DOWN:
temp[0] = (int) arg1.getX();
temp[1] = (int) (y - arg0.getTop());
mLayoutGroup.bringChildToFront(arg0);
arg0.postInvalidate();
break;
case MotionEvent.ACTION_MOVE:
int left = x - temp[0];
int top = y - temp[1];
int right = left + arg0.getWidth();
int bottom = top + arg0.getHeight();
arg0.layout(left, top, right, bottom);
arg0.postInvalidate();
break;
// 终止触摸时刻
case MotionEvent.ACTION_UP:
int imagX = (int) arg0.getX() + arg0.getWidth() / 2;
int imagY = (int) arg0.getY() + arg0.getHeight() / 2;
int mllX = (int) main_ll.getX();
int mllY = (int) main_ll.getY();
int mllW = main_ll.getWidth();
int mllH = main_ll.getHeight();
if (imagX > mllX && imagX < (mllX + mllW) && imagY > mllY
&& imagY < (mllY + mllH)) {
ImageView imageView = new ImageView(Test_TocuhActivity.this);
imageView
.setImageDrawable(((ImageView) arg0).getDrawable());
main_ll.addView(imageView);
}
mPop.dismiss();
break;
}
return false;
}
};
void alert() {
new AlertDialog.Builder(this)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
}).setTitle("test button").setMessage("test test test!!!")
.show();
}
private void initPopWindow() {
if (mPop == null) {
mPop = new PopupWindow(mLayoutGroup, LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT, true);
}
if (mPop.isShowing()) {
mPop.dismiss();
}
}
}
Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="长按Anroid小人后,拖入蓝色区域内,有缺陷,望各位高手改善" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/sym_def_app_icon" />
<LinearLayout
android:id="@+id/main_ll"
android:layout_width="300dip"
android:layout_height="300dip"
android:layout_gravity="center"
android:background="@android:color/holo_blue_dark" >
</LinearLayout>
<Button
android:id="@+id/main_btn"
android:layout_width="100dip"
android:layout_height="100dip"
android:text="来点我啊" />
</LinearLayout>
--------------------编程问答-------------------- 没有大师么? --------------------编程问答-------------------- 跟楼主碰到相同问题
求答案 --------------------编程问答-------------------- 顶啊 没有人会么 --------------------编程问答-------------------- 不知楼主现在解决了没,求方法
补充:移动开发 , Android