0

I am using a menu in my application same as this post... I am using a gallery view for display my menu items.

Text Gallery on Android?

Problem is that, i implement onItemSelected listener for gallery, so that when new item is selected data related to that topic loaded. But i also want to allow user to scroll the gallery fully. But each time when user move to next item onItemSelected() function called and it start loading data.

All i want to do is to put some delay in onItemSelected() function, so that if in between that delay user scroll next item than there is no need to load data of previous but for the current. Time may be 1 second. If user dose not go for next item in 1 second, that data of that item must be loaded.

Can anyone help? I thought to start a thread, but each time for onItemSelected() there will be new thread...

I tried this too

public class TimerThreadForCategoriesMenu extends Thread{

int old = -1;
int cur = -1;
CategoriesActivity catAct = null;

public TimerThreadForCategoriesMenu(CategoriesActivity act , int cu) {
    this.cur = cu;
    old = cu;
    this.catAct = act;

}
@Override
public void run() {
    Looper.prepare();

    do{
        old = this.cur;
        for(int i = 0; i<15; i++){
            try{
                Thread.sleep(100);

            }catch (Exception e) {
                e.printStackTrace();
            }
        }

    }while(cur != old);
    catAct.performTask();
    Looper.loop();  
}

public void setCur (int curr){
    this.cur = curr;
}

}

And in OnItemSelected()

      if(timer == null){  

        timer = new TimerThreadForCategoriesMenu(this, arg2);
        timer.start();
    }
    timer.setCur(curInd);

Found Exception:

11-24 16:48:50.046: ERROR/AndroidRuntime(8049): Uncaught handler: thread Thread-8 exiting due to uncaught exception
11-24 16:48:50.126: ERROR/AndroidRuntime(8049): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-24 16:48:50.126: ERROR/AndroidRuntime(8049):     at android.view.ViewRoot.checkThread(ViewRoot.java:2683)
11-24 16:48:50.126: ERROR/AndroidRuntime(8049):     at android.view.ViewRoot.invalidateChild(ViewRoot.java:570)
11-24 16:48:50.126: ERROR/AndroidRuntime(8049):     at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:596)
11-24 16:48:50.126: ERROR/AndroidRuntime(8049):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:2396)
11-24 16:48:50.126: ERROR/AndroidRuntime(8049):     at android.view.View.invalidate(View.java:4945)
11-24 16:48:50.126: ERROR/AndroidRuntime(8049):     at

Community
  • 1
  • 1
Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105

2 Answers2

1

I had exactly the same requirement, since onItemSelected will be called so often if the user spinning through the Gallery I wanted to avoid these events. I implemented a simple delay mechanism with Handler as follows:

Define a handler instance in the Activity class and override handleMessage which should have the onItemSelected logic. Here, I've the msg.obj contains the text of the selectedItem from the Gallery

Handler handler = new Handler() {
    public void handleMessage(Message msg) {
                    // Add your OnItemSelected code here
        Log.d("Gallery", msg.obj + "");
        Toast.makeText(MainActivity.this, msg.obj + "", Toast.LENGTH_SHORT)
                .show();
    }
};

This is just an arbitrary constant to refer to the message that I send from OnItemSelected

public static final int MSG_ITEM_SELECTION = 1;

In onItemSelected I first remove any previous message to avoid firing the event while user spinning the Gallery, and then add a new message object with the selected item text and post it with a 500 ms delay.

public void onItemSelected(AdapterView<?> adapter, View view,
                int position, long id) {
            handler.removeMessages(MSG_ITEM_SELECTION);
            Message msg = handler.obtainMessage(MSG_ITEM_SELECTION);
            msg.obj = ((TextView) view).getText();
            handler.sendMessageDelayed(msg, 500);
        }

This code works fine for me, and I only receive the onItemSelected when a Gallery item is really selected after spinning stops.

iTech
  • 18,192
  • 4
  • 57
  • 80
1

use a Handler and postDelayed() a Runnable

Edit:

It depends on what processing you wish to do. Have a look at Looper in the reference. The UI has its own Looper so you don't need to create one. Just create a Handler, however any Runnable posted to the UI Thread Handler will be run on the UI thread. If you are doing stuff that takes a while to complete, create you're own Thread with a Handler and post Runnables to that. http://developer.android.com/reference/android/os/Looper.html

Edit:

So create members in your Activity.

Handler handler;
MyLooper mylooper;

Your thread looper.

class MyLooper extends Thread {
  public Handler handler;

  public void run() {
      Looper.prepare();

      handler = new Handler() {
          public void handleMessage(Message msg) {}
      };

      Looper.loop();
  }
}

In your Activity.onCreate.

handler = new Handler();
mylooper = new MyLooper();

Now create Runnables.

Runnable processinfo = new Runnable() {
    public void run() {
        //  your processing here
    }
}

Runnable updateui = new Runnable() {
    public void run() {
        //  update ui component here
    }
}

Now to get these Runnables to execute.

mylooper.handler.postDelayed(processinfo, 1000);

handler.post(updateui);

You will need some logic to handle cancelling the Runnable based on the needs of your delay.

techi.services
  • 8,473
  • 4
  • 39
  • 42