I have a button. On click, I am refreshing a list.
What I am experiencing:
The state_pressed color stays for a big while (meaning the UI thread blocked)
(Note: the button background is defined such as on state_pressed="true" the color changes)
Question:
How can I click the button to refresh the list, without blocking the UI?
My Code:
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
refreshListView(data);
}
});
public void refreshListView(ArrayList data) {
// I am setting setNotifyOnChange() to false
// to prevent notifyDataSetChanged() to be triggered twice, by clear() and by addAll()
mArrayAdapter.setNotifyOnChange(false)
mArrayAdapter.clear();
mArrayAdapter.addAll(data);
// I am triggering notifyDataSetChanged() explicitely
mArrayAdapter.notifyDataSetChanged();
mListView.post(new Runnable() {
@Override
public void run() {
// this is to reset to the top of the list, after the list has been populated
mListView.setSelectionAfterHeaderView();
}
});
}
EDIT:
I think I solved it by calling the refresh code from a View.post(), but I wonder if this should always be needed:
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mListView.post(new Runnable() {
@Override
public void run() {
refreshListView(data);
}
});
}
});