开发者

View State lost with click

开发者 https://www.devze.com 2023-01-09 14:12 出处:网络
I have created a custom View that is essentially a Gallery object that can be hidden and shown (using Vie开发者_开发技巧w.VISIBLE and View.GONE) by clicking on a TextView.

I have created a custom View that is essentially a Gallery object that can be hidden and shown (using Vie开发者_开发技巧w.VISIBLE and View.GONE) by clicking on a TextView.

The images I place in this gallery have a custom background StateListDrawable selector. Everything works fine except the state of the images in the gallery seems to get lost when the gallery goes from View.GONE to View.VISIBLE, and that the selector that is drawn is the default drawable (translucent_box).

These are the items in my selector XML:

<item android:drawable="@drawable/red_box" android:state_pressed="true" />
<item android:drawable="@drawable/orange_box" android:state_selected="true" android:state_focused="true" />
<item android:drawable="@drawable/blue_box" android:state_selected="true" />
<item android:drawable="@drawable/translucent_box" />

The strangest thing is that after returning from a call to startActivity, the state gets restored and everything displays fine until I hide and show the gallery again.

I have tried playing around with adding different states in the selector and programmatically trying to refresh the view... to no avail.

Any ideas? I'm sure it has got to be something trivial...

Thanks in advance!


I'm not sure it's relevant - but do you need to implement View.verifyDrawable? The docs aren't very clear as to when you need to do that.

startActivity will trigger an animation by default - which is probably what is forcing the view to refresh correctly. Another possible hack around this would be to trigger an empty animation of some sort.


I was able to find a workaround for my case. I'm not sure it is the best way and I still don't know what is causing the behavior, but I was able to get what I wanted by calling notifyDataSetChanged() on my gallery's adapter.

If anyone has a better suggestion on how to solve the problem (mine seems like it might be a little overkill) or even a description of why this is happening, I would appreciate it... I am very curious about what is going on behind the scenes.

0

精彩评论

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