简体   繁体   English

可运行不会运行

[英]Runnable wont run

im trying to check when my user stop scrolling. 我试图检查我的用户何时停止滚动。

To this i use this method: 为此,我使用这种方法:

commentsLayout.setScrollViewListener(new ScrollViewListener() {

            @Override
            public void onScrollChanged(WallScrollView scrollView, int x, int y,
                    int oldx, int oldy) {
                // TODO Auto-generated method stub

                oldCommentsYDiefference = oldy;
                commentsYDiefference = y;

                             //do something
}

I noticed that when the user stop scrolling the difference between oldCommentsYDiefference and commentsYDiefference is always 1. 我注意到,当用户停止滚动时, oldCommentsYDiefferencecommentsYDiefference之间的差异始终为1。

So in order to check when my user stop scrolling i thought about creating a thread that checks when the difference of this two number is 1 by using this code: 因此,为了检查用户何时停止滚动,我想到了使用以下代码创建一个线程来检查这两个数字的差为1的线程:

public void checkFromScrolling()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {

                Log.i("commentsYDiefference", commentsYDiefference+"");
                Log.i("oldCommentsYDiefference", oldCommentsYDiefference+"");

                if(commentsYDiefference - oldCommentsYDiefference == 1 || oldCommentsYDiefference - commentsYDiefference == 1)
                {

                    if(checkWallScroll)
                        new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                                         //do things

                            oldCommentsYDiefference = commentsYDiefference;

                            checkWallScroll = true;

                            }
                    }, 2000);

                    checkWallScroll = false;
                }
            }
        }).start();
    }

I call checkFromScrolling() in my onCreate. 我在onCreate中调用checkFromScrolling()

My problem is that the Runnable is running only once and not all the time. 我的问题是Runnable只运行一次,而不是一直运行。 I know this because i get only one call in my Log reports for 我知道这是因为我在“日志”报告中仅收到一个呼叫

Log.i("commentsYDiefference", commentsYDiefference+"");
Log.i("oldCommentsYDiefference", oldCommentsYDiefference+"");

How to make Runnable run all the time the activity is open? 如何使Runnable在活动打开时始终运行?

You could add a while-loop checking if a boolean variable (keepRunning) is true. 您可以添加while循环检查布尔变量(keepRunning)是否为true。 And then you have to set it to false before your activity is closed. 然后,必须在活动关闭之前将其设置为false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM