Highlight layout after delay?
I've created my own compound widget, something just like this:
<LinearLayout>
<TextView />
<Button />
</LinearLayout>
I'm putting 15 of these in a ScrollView. I want it to behave similar to a ListView (I cannot use a ListView directly for this task).
I need the each widget to highlight when pressed. I've gotten this to work, but it's too sensitive - as soon as my finger hits the layout, it highlights - I'd like there to be a delay befo开发者_Go百科re highlight to discern between a real tap, and just a scroll gesture.
For example, in ListView, when you drag your finger to scroll, the row items don't get highlighted unless you leave your finger on the row for a second.
This is how I've set the background of each of my compound items:
private void setBg() {
ColorDrawable blr = new ColorDrawable(0xFFFFFFFF);
Drawable pressed = getContext().getResources().getDrawable(android.R.drawable.list_selector_background);
StateListDrawable bg = new StateListDrawable();
bg.addState(View.PRESSED_ENABLED_STATE_SET, pressed);
bg.addState(View.FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, pressed);
bg.addState(View.PRESSED_ENABLED_FOCUSED_STATE_SET, pressed);
bg.addState(View.ENABLED_WINDOW_FOCUSED_STATE_SET, pressed);
bg.addState(View.ENABLED_STATE_SET, blr);
bg.addState(View.SELECTED_WINDOW_FOCUSED_STATE_SET, blr);
setBackgroundDrawable(bg);
}
so I'm not sure if I just have set one of the above background drawable states incorrectly - or does ListView do some sort of touch handling internally for that tap delay?
Thanks
Try using an Animation or Transition, run animation or transition start when touch down, and stop or reset when touch is up or cancel. Put a delay on the first frame/item in the list.
http://developer.android.com/guide/topics/graphics/2d-graphics.html#frame-animation
Romain Guy's presentation from Google IO 2009 has been helpful to me in a similar situation:
Make your Android UI Fast and Efficient (about 90 seconds in)
The ViewHolder trick mentioned can help a ton with performance.
Also, showing a temporary Drawable while the image loads from cache can let you scroll fast even if the image hasn't loaded yet, then you can use an animation to (for example) fade in the image when returned from cache.
This seems to be the approach they take in apps such as the Android Market.
精彩评论