TouchEvent executing both if and else part

送分小仙女□ 提交于 2019-12-23 18:01:23

问题


In the code below the problem i m facing is that in onTouchListener method both if and else part is getting executed each time. Anyone have idea why is it happening like that? and aim of my project is on touch event on anywhere in on the webview the dock layout must appear on another touch it must disappear.Can anyone help regarding this?

Thanks in advance ..

     public class MathiasdockActivity extends Activity {
    /** Called when the activity is first created. */
    RelativeLayout upperdock,parent;
    LinearLayout tocbottom,tocparent;
    boolean flag=true;
    WebView wv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        parent=(RelativeLayout) findViewById(R.id.rl1);
        upperdock=(RelativeLayout) findViewById(R.id.upperdock);
        tocparent=(LinearLayout) findViewById(R.id.tocparent);
        tocbottom=(LinearLayout) findViewById(R.id.linearLayout3);
        upperdock.setVisibility(RelativeLayout.INVISIBLE);
        tocparent.setVisibility(LinearLayout.INVISIBLE);
        tocbottom.setVisibility(LinearLayout.INVISIBLE);
        wv = (WebView) findViewById(R.id.webView1);  
        wv.loadUrl("file:///android_asset/hotspot/07-03.html");
        wv.getSettings().setJavaScriptEnabled(true);


        wv.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                    if(flag)
                    {
                            Toast toast1 = Toast.makeText(getBaseContext(), Boolean.toString(flag) + "if", 1000);
                            toast1.show();
                            upperdock.bringToFront();
                            tocparent.bringToFront();
                            tocbottom.bringToFront();
                            upperdock.setVisibility(RelativeLayout.VISIBLE);
                            tocparent.setVisibility(LinearLayout.VISIBLE);
                            tocbottom.setVisibility(LinearLayout.VISIBLE);
                            flag=false;
                    }
                    else
                    {
                            Toast toast2 = Toast.makeText(getBaseContext(), Boolean.toString(flag) + "else", 1000);
                            toast2.show();
                            wv.bringToFront();
                            upperdock.setVisibility(RelativeLayout.INVISIBLE);
                            tocparent.setVisibility(LinearLayout.INVISIBLE);
                            tocbottom.setVisibility(LinearLayout.INVISIBLE);
                            flag=true;  

                    }

                    return flag;
            }
        });
    }
   }

回答1:


Its because OnTouchListener calls continuously, so you need to check that user touches first time by MotionEvent.ACTION_DOWN

wv.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

        if(event.getAction()== MotionEvent.ACTION_DOWN)
        {
                if(flag)
                {
                   Toast toast1 = Toast.makeText(getBaseContext(), Boolean.toString(flag) + "if", 1000);
                   toast1.show();
                   upperdock.bringToFront();
                   tocparent.bringToFront();
                   tocbottom.bringToFront();
                   upperdock.setVisibility(RelativeLayout.VISIBLE);
                   tocparent.setVisibility(LinearLayout.VISIBLE);
                   tocbottom.setVisibility(LinearLayout.VISIBLE);
                   flag=false;
                }
                else
                {
                   Toast toast2 = Toast.makeText(getBaseContext(), Boolean.toString(flag) + "else", 1000);
                   toast2.show();
                   wv.bringToFront();
                   upperdock.setVisibility(RelativeLayout.INVISIBLE);
                   tocparent.setVisibility(LinearLayout.INVISIBLE);
                   tocbottom.setVisibility(LinearLayout.INVISIBLE);
                   flag=true;
                }
          }
              return flag;
        }
    });



回答2:


There are many action for touch event so please use it with if condition.

1.MotionEvent.ACTION_DOWN
2.MotionEvent.ACTION_MOVE
3.MotionEvent.ACTION_UP
.
.
.
etc

and always return false;

Update

if(event.getAction()== MotionEvent.ACTION_DOWN)
{
  //when you touch on screen
}
else if(event.getAction()== MotionEvent.ACTION_MOVE)
{
    //when you move your finger on screen
}
else if(event.getAction()== MotionEvent.ACTION_UP)
{
  //when you release your finger from screen
}



回答3:


It only looks like both are being executed at the same time. In reality, they are executed separately, but because the touch event is being fired so rapidly, it appears that they're both executed at the same time. What you should be doing is detecting the type of the motion event first e.g., MotionEvent.ACTION_MOVE or something. Otherwise, all motion events are handled by your listener code.

Of course the reason both if and else are called in your code is because you're setting the flag to true and false in each statement, so that the execution of the statements alternates.



来源:https://stackoverflow.com/questions/10811131/touchevent-executing-both-if-and-else-part

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!