How to make so that when you quickly switch to ListView, only the action corresponding to the item on which the user was delayed for some time (for example, 300 ms) was performed, and those that quickly turned over were ignored?

    4 answers 4

    private Runnable mMyRunnable = new Runnable() { @Override public void run() { //Делаем что нужно } }; Handler myHandler = new Handler(); myHandler.postDelayed(mMyRunnable, 300);//Даем команду выполнить через 300мс 

    not that?

    • Well, almost that)) Just how to make it so that only one task can be performed in the Handler, that is, if you post the next one, and the previous one is paused, will the old one be deleted? - anutakay
    • No, postDelayed in this case must be tied to one of the View-classes. But yes, you can and so do delays. - DroidAlex

    Solution found. I did as advised by @katso , but I also prescribed:

     public void onItemSelected(AdapterView<?> parent, View v, int position, long id) { savedItem = position; //Удаляем все отложенные задания myHandler.removeCallbacks(mMyRunnable); //Задаем новое myHandler.postDelayed(mMyRunnable,300); } 

    Here, the number of the selected item is saved to the global savedItem , and in code is executed depending on this saved number.

      You can delay execution by calling Thread.sleep (3000), where 3000 is milliseconds, that is, 3 seconds. But to delay the flow of UI (it’s the main one, it’s the current default) is a brute force, never do that. You can write custom animation for the selected View and animation for the list itself.

      For example, by selecting an item in the list, the animation for the selected view will increase it, and the animation of the list will remove all others. But here you need to think over everything in detail, because their interaction is sometimes not an easy task.

      • I just need the UI to be free. He begins to perform heavy onItemSelected (video launch) for each item that gets into focus, so the list animation slows down. You can, of course, make a launch on click, but this does not fit into the concept of the interface. You can also run a heavy task in a separate thread with the necessary delay, and if the task for the next item starts, the flow stops and clears, and the next task goes there ... And VideoView.start () will not start in the UI? But then it is possible that the thread only sends a message after a pause. Some crutches. - anutakay
      • Describe the task in more detail and in simple language. It was hard to understand exactly what you want, so the answer was in general terms. - DroidAlex
      • Sorry, it's me ... plunged)) I will try again)) When the ListView item is selected, the onItemSelected function of the OnItemSelectedListener is called. So? If you switch items quickly, then the corresponding functions will call quickly one after the other. For example, my video quickly switches to the menu items. From this list itself slows down. It is necessary that the video be launched only when the user is delayed on the list item. Crutches can be made, but is it not possible to listen only to a long retention of choice? - anutakay
      • Now clearer. Well, there is onLongItemSelected, but this is a long click on the item (tap), which is not suitable in your case. It is not clear why they are reproduced right away from you, but oh well. There simply can not guess the delay in this case. How long is this? Maybe I chose 300ms, then 500, and can vary very differently. Try pochamanit with selectedItem.postDeleyed (), but I think you need to change the idea itself. - DroidAlex
      • Change the idea does not work, it is not mine. The User case should be the following: the user presses the up / down keys on the remote - the channels are switched, that is, as usual on a regular TV. In ListView, accordingly, the list of channels that can be shown, and can be hidden. Something like this.)) I found a solution, I will write below. Thanks for the help =) - anutakay

      Try this.

      1. Get a list of pending tasks.
      2. When the user comes to the ListView item, add the task to the list and start the timer.
      3. When the user leaves the ListView, drop the task from the list (if it is still there) and stop the timer (if it is not stopped yet).
      4. When the timer arrives, if the task has not yet been dropped from the list, perform the task and stop the timer.

      It seems to work.

      If you have the opportunity to subscribe to the "long" hover / touch, of course, use it, and do not reinvent the bike.

      If you use the above @katso postDelayed , you can do this:

      1. We assign a global type ID to each task, say, long .
      2. In new Runnable() store a unique ID in the final-field; remember this ID in the long activeID field.
      3. In the Run method, we check if its ID activeID ; if not, stop execution.
      • @VladD, thanks for the reply. - anutakay