I am using a menu in my application same as this post… I am using a gallery view for display my menu items.
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
use a
HandlerandpostDelayed()aRunnableEdit:
It depends on what processing you wish to do.
Have a look at
Looperin the reference. The UI has its ownLooperso you don’t need to create one. Just create aHandler, however anyRunnableposted 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 ownThreadwith aHandlerand postRunnablesto that.http://developer.android.com/reference/android/os/Looper.html
Edit:
So create members in your Activity.
Your thread looper.
In your
Activity.onCreate.Now create
Runnables.Now to get these
Runnablesto execute.You will need some logic to handle cancelling the
Runnablebased on the needs of your delay.