开发者

Android ListView with Custom List Selector causing Flickring problem

I'm setting a selector.xml to the ListView Selector :

<item android:state_focused="false" android:state_selected="true"
    android:state_pressed="true" android:drawable="@drawable/timeline_selected_rect"/>

<item android:state_focused="false" android:state_selected="true"
    android:state_pr开发者_StackOverflow中文版essed="false" android:drawable="@drawable/timeline_selected_rect"/>

<item android:state_focused="true" android:state_selected="true"
    android:state_pressed="true" android:drawable="@drawable/timeline_selected_rect"/>

but setting this is causing the TextView flickr over selection. this thing is not happening when the Default selector is used... whats wrong with this selector.

I have even added android:cacheColorHint="#00000000" as provided info by some other blogs.


I recently ran into this problem as well and found the solution by analyzing the Android source code.

You need to remove the android:state_selected="true" attributes since checking that state is unneeded. Once a list item loses it's focus, it also makes 'selected = false'. Since focus=false & selected=true will never occur, your first 2 <items> in the selector will never be shown.

Think of the tap like a mouse click with an onPressDown and onPressUp event. So your new selector.xml should looks something like this to avoid the flickering:

<!-- {comment copied directly from Android source code}  
  Even though these two point to the same resource, have two states 
  so the drawable will invalidate itself when coming out of pressed state. 
-->
<item android:state_pressed="true" android:state_focused="true" 
  android:drawable="@drawable/timeline_selected_rect"/>
<item android:state_pressed="true" android:state_focused="false"
  android:drawable="@drawable/timeline_selected_rect" />

<item android:state_focused="true"
  android:drawable="@drawable/timeline_selected_rect" />

<item android:state_window_focused="false"
  android:drawable="@android:color/transparent" />

As an aside, you should seriously consider creating a separate drawable for the pressed state so that you give a bit of visual feedback. It is always nicer to see "something happen" when interacting with the UI; whether that is drawing a button in a "down" state while it is being pressed or changing the color slightly while tapping on a list item, the visual feedback is good practice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜