سؤال

Q1) Looking at original ListView state drawable for listSelector we have something like this (I stripped 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've been doing it like this on my app:

<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's much more simple. Do you notice any situation where the first one will work better than my own version? I just want to make sure I'm not missing something important cause I pefer to keep my code as short as possible...

Q2) I see many state drawables with <item android:drawable="<TRANSPARENT>" android:state_window_focused="false"/> as the top item but I can't understand why is it needed. The state_window_focused always confused me a bit... Is it really necessary?

هل كانت مفيدة؟

المحلول

I had to put this into an answer, even though I'm not sure about it, but that's what I think:

I'll start with Q2:

state_window_focused corresponds to the default behaviour ("window" visible, but unfocused) and I think it's used just to point out the default (TRANSPARENT).

About Q1:

DRAWABLE2 will be visualised in both cases and under the same conditions - focused and unpressed, because:

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 the D2 while focused, but in your case also while unpressed.

DRAWABLE1 will be visible only while pressed in each and every case. The difference is that in the first case, the two additional states are given too android:state_focused="true/false", which doesn't make any sense, so the two lines in Case 1 can be shrunk to one (exactly the one you have):

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

So, to sum things up:

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 equivalen to:

<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 I can see so far, is that in the first case, the DRAWABLE2 will be shown only when focused (no matter if pressed or not), but in your case it has to be also unpressed and that's the only condition which is different.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top