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