What is the difference in behavior from these slightly different state indicators?

Q1) Looking at the initial ListView state allocated for listSelector , we have something like this (I removed some code to simplify the example):

 <selector> <item android:drawable="<TRANSPARENT>" android:state_window_focused="false"/> <item android:drawable="<DRAWABLE_1>" android:state_focused="true" android:state_pressed="true"/> <item android:drawable="<DRAWABLE_1>" android:state_focused="false" android:state_pressed="true"/> <item android:drawable="<DRAWABLE_2>" android:state_focused="true"/> </selector> 

But I did it like this in my application:

 <selector> <item android:drawable="<DRAWABLE_2>" android:state_pressed="false" android:state_focused="true"/> <item android:drawable="<DRAWABLE_1>" android:state_pressed="true"/> <item android:drawable="<TRANSPARENT>" /> </selector> 

Which seems to produce the same result, and it is much simpler. Have you noticed any situation where the first will work better than my own version? I just want to make sure I'm missing some important reason why I want my code to be as short as possible ...

Q2) I see many status indicators with <item android:drawable="<TRANSPARENT>" android:state_window_focused="false"/> as the top element, but I can’t understand why it is needed. state_window_focused always confused me a little ... Is it really necessary?

+8
android android-drawable
source share
1 answer

I had to answer this question, although I'm not sure about it, but this is what I think:

I'll start with Q2:

state_window_focused corresponds to the default behavior (the "window" is visible but not focused), and I think it used only to specify the default value ( TRANSPARENT ).

About Q1:

DRAWABLE2 will be displayed both in both cases and under the same conditions - focused and not pressed, because:

The first case:

 <item android:drawable="<DRAWABLE_2>" android:state_focused="true"/> 

Second case:

 `<item android:drawable="<DRAWABLE_2>" android:state_focused="true" android:state_pressed="false"/>` 

We have D2 when it is focused, but in your case also not pressed.

DRAWABLE1 will only be displayed when pressed in each case. The difference is that in the first case, two additional states are also given by android:state_focused="true/false" , which makes no sense, so the two lines in case 1 can be compressed to one (exactly the one you have):

 <item android:drawable="<DRAWABLE_1>" android:state_pressed="true"/> 

So, to summarize:

Case 1 - you have:

 <selector> <item android:drawable="<TRANSPARENT>" android:state_window_focused="false"/> <item android:drawable="<DRAWABLE_1>" android:state_focused="true" android:state_pressed="true"/> <item android:drawable="<DRAWABLE_1>" android:state_focused="false" android:state_pressed="true"/> <item android:drawable="<DRAWABLE_2>" android:state_focused="true"/> </selector> 

Which is equivalent:

 <selector> <item android:drawable="<TRANSPARENT>" android:state_window_focused="false"/> <item android:drawable="<DRAWABLE_1>" android:state_pressed="true"/> <item android:drawable="<DRAWABLE_2>" android:state_focused="true"/> </selector> 

And your case (case 2), for comparison:

 <selector> <item android:drawable="<DRAWABLE_2>" android:state_pressed="false" android:state_focused="true"/> <item android:drawable="<DRAWABLE_1>" android:state_pressed="true"/> <item android:drawable="<TRANSPARENT>" /> </selector> 

The only difference that I see so far is that in the first case, DRAWABLE2 will only be displayed when focusing (whether pressed or not), but in your case it should also be unpressed and that the only condition that is different .

+2
source share

All Articles