开发者

Android - OnTouch Do Work

开发者 https://www.devze.com 2023-02-20 08:31 出处:网络
I have some code that has got me very close to what Im trying to achieve.But there are a few issues.What I want to do is, when a Button is pressed and held, it will continuously call a method AdvanceL

I have some code that has got me very close to what Im trying to achieve. But there are a few issues. What I want to do is, when a Button is pressed and held, it will continuously call a method AdvanceLog(). The problem is, the ACTION_DOWN motion event does not continue to get called. What happens is, when you press and hold, the first motion is ACTION_DOWN and every motion thereafter while still holding down is ACTION_MOVE. This is causing some is开发者_JS百科sues, as I am not REALLY looking for the move action, more so just the repetetive down action. Any suggestions?

advanceButton.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) 
            {
                int counter=0;
                if(event.getAction()==MotionEvent.ACTION_DOWN || event.getAction()==MotionEvent.ACTION_MOVE)
                {
                    fwdTouchCounter++;
                    AdvanceLog(true, true);
                    try 
                    {
                        if(fwdTouchCounter>5)                           
                            Thread.sleep(30);
                        else
                            Thread.sleep(800);
                    } 
                    catch (InterruptedException e) 
                    {

                        e.printStackTrace();
                    }
                }
                else if(event.getAction()==MotionEvent.ACTION_UP)
                    fwdTouchCounter=0;

                return false;

            }
        });


On your ACTION_DOWN touch event, fire off a thread that calls a local method. Inside this method constantly read a class variable (boolean is easiest) and if that variable, let's call it m_advanceLog is true, continue to process (advancing your log I'm guessing). In your ACTION_UP touch event, set m_advanceLog to false which should stop your loop and allow the thread to exit.

Keep in mind that this is only a good idea if there aren't user interface changes, othwerwise some additional work is needed due to the thread interaction.

0

精彩评论

暂无评论...
验证码 换一张
取 消