-1

在我的应用程序中,我有一个弹出菜单,当我单击菜单项时它会收集 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);

            }

任何帮助表示赞赏。

我的弹出主 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>
4

4 回答 4

1

我怀疑 popName 和 popImage 在您的活动或片段 XML 中的 popMain 上。

如果他们在这个布局上,这是很正常的行为。

因为 Layout 路由在处理它之后事件它的子节点。

删除这段代码

   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 http://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent )来实现您的 LinearLayout

于 2013-01-23T07:45:49.537 回答
0

将您的“popUpMenu”实例保留为私有类成员,并在调用“doAction()”之前检查它是否已经显示

于 2013-01-23T07:39:32.253 回答
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 LinearLayoutpopupName TextView

于 2013-01-23T10:29:16.077 回答
0

简单的 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);

                }
            }); 
于 2013-01-23T11:01:54.780 回答