Reputation: 3478
I have a ListView
with custom rows. They have different background colors, set ind the getView()
method of an ArrayAdapter
:
public View getView(int position, View convertView, ViewGroup parent) {
...
bgResourceId = R.color.white;
if (items.get(position).status.contentEquals("ok")) {
bgResourceId = R.color.green;
} else items.get(position).status.contentEquals("error")) {
bgResourceId = R.color.red;
}
row.setBackgroundResource(bgResourceId);
return row;
}
Clicking on a row launches a new Activity. After finishing the new Actvity, we return to the Activity with the ListView
and at his point I need to highlight one of the rows.
Inside the onResume()
method, NOT after touching or clicking the row.
I do know which row (index) will get highlihgted.
Just a simple animation - maybe blink for one second, and after that, return to its original background color. Something like this:
Is that possible?
Upvotes: 0
Views: 1305
Reputation: 3832
This is not as trivial as it may seem. You have to
Make sure that the timing is right. onResume
will not let you access item views yet, therefore you should use onWindowsFocusChanged
. Put this in your activity:
Handler handler = new Handler();
int blink = 4;
int regularBg = android.R.color.holo_orange_light;
int blinkingBg = android.R.color.holo_orange_dark;
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
final View view = adapter.getViewByPosition(blink, listView);
view.setBackgroundResource(blinkingBg);
handler.postDelayed(new Runnable() {
@Override
public void run() {
view.setBackgroundResource(regularBg);
listView.setOnScrollListener(null);
}
}, 1000);
}
}
Access item views through a custom method you add to the adapter (won't work through the regular getView
). It is from over here.
View getViewByPosition(int pos, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (pos < firstListItemPosition || pos > lastListItemPosition) {
return listView.getAdapter().getView(pos, null, listView);
} else {
final int childIndex = pos - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
Make sure that the item you want to highlight is visible under all circumstances. If you cannot guarantee this, because there may be very small devices or the item you want to highlight is further down the list than in the list you show in your question, you will have to scroll to the correct position first. Only after the scroll is finished, you can safely change the item background. Therefore you would have to implement an onScrollListener
for your ListView
. For now, until you tell me otherwise, I assume that this is not necessary.
Here is how it looks:
Upvotes: 1