在我的应用程序中我有一个弹出菜单,当我点击menuitem它收集gps位置时,问题是当我双击菜单时触发两次但我只是在onclick上执行 不确定我在哪里做错了
LinearLayout popupmain = (LinearLayout) view
.findViewById(R.id.popupmain);
ImageView popupImage = (ImageView) view.findViewById(R.id.popupimg);
TextView popupName = (TextView) view.findViewById(R.id.popupname);
popupImage.setImageResource(popUpMenu.getImageResource());
popupName.setText(popUpMenu.getMenuName());
popupName.setTag(popUpMenu);
popupmain.setTag(popUpMenu);
popupName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
doAction(v, activity);
}
});
popupmain.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
doAction(v, activity);
}
});
return view;
}
public void doAction(View v, Activity activity) {
Context myContext = v.getContext();
PopUpMenu popUpMenu = (PopUpMenu) v.getTag();
String result = popUpMenu.getMenuName();
if (result != null
&& result.equalsIgnoreCase(myContext.getResources().getString(
R.string.savecurrentlocation))) {
getLocation();
}
private void getLocation() {
manager = (LocationManager) parentActivity
.getSystemService(Context.LOCATION_SERVICE);
listener = new ManageLocation();
listener.setSucessCallBack(this, "setLocation");
listener.setFailureCallBack(this, "setNoLocation");
manager.requestLocationUpdates(
new Utils().getCurrentPlaceLocationProvider(manager), 0, 0,
listener);
}
感谢任何帮助。
我的popupmain xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popupmain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="5dp" >
<ImageView
android:id="@+id/popupimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/popupname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="8dp"
android:text="@string/default_date"
android:textColor="#000000"
android:textSize="15dp"
android:typeface="serif" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:1)
我怀疑popName和popImage是在您的活动或片段XML中的popMain上。
如果他们在这个布局上,这是非常正常的行为。
因为布局路线在处理后将其子事件发送给它。
删除这段代码
popupmain.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
doAction(v, activity);
}
});
或者像这样添加控制逻辑(我不知道你的主要想法)
popupName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(popupMainOpened)
doAction(v, activity);
}
});
popupmain.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(!popupMainOpened)
doAction(v, activity);
}
});
或通过扩展LinearLayout并覆盖onTouchEvent来实现LinearLayout http://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent)
答案 1 :(得分:0)
将'popUpMenu'实例保留为私有类成员,并在调用'doAction()'之前检查它是否已显示
答案 2 :(得分:0)
请注意,这是未经测试的代码。我只是在记事本中输入了它。我不确定它是否可以在中运行但是应该帮助您理解它背后的逻辑。当然,格式化有点偏。必要时进行调整。
public class YourAdapter extends BaseAdapter {
boolean runningAction = false; // DECLARE AS A GLOBAL INSTANCE
// IN YOUR GETVIEW()
LinearLayout popupmain = (LinearLayout) view.findViewById(R.id.popupmain);
ImageView popupImage = (ImageView) view.findViewById(R.id.popupimg);
TextView popupName = (TextView) view.findViewById(R.id.popupname);
popupImage.setImageResource(popUpMenu.getImageResource());
popupName.setText(popUpMenu.getMenuName());
// I AM SKIPPING THE REST
popupmain.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (runningAction == false) {
doAction(v, activity);
} else {
// DO NOTHING OR SHOW A TOAST THAT A FUNCTION IS IN PROGRESS
}
}
});
return view;
}
// THE doAction()
public void doAction(View v, Activity activity) {
// TOGGLE THE boolean flag runningAction
runningAction = true;
Context myContext = v.getContext();
PopUpMenu popUpMenu = (PopUpMenu) v.getTag();
String result = popUpMenu.getMenuName();
if (result != null
&& result.equalsIgnoreCase(myContext.getResources().getString(
R.string.savecurrentlocation))) {
getLocation();
}
// THE getLocation()
private void getLocation() {
manager = (LocationManager) parentActivity
.getSystemService(Context.LOCATION_SERVICE);
listener = new ManageLocation();
listener.setSucessCallBack(this, "setLocation");
listener.setFailureCallBack(this, "setNoLocation");
manager.requestLocationUpdates(
new Utils().getCurrentPlaceLocationProvider(manager), 0, 0,
listener);
// TOGGLE THE boolean flag runningAction TO PREPARE FOR NEXT LOCATION UPDATE
runningAction = false;
}
}
另请注意,我只将ClickListener设置为popupmain LinearLayout
而不是popupName TextView
答案 3 :(得分:0)
simple v.setEnabled(false);解决了我的问题
popupName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popupName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.setEnabled(false);
performAction(v, activity);
}
});
popupmain.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.setEnabled(false);
performAction(v, activity);
}
});
performAction(v, activity);
}
});
popupmain.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.setEnabled(false);
performAction(v, activity);
}
});