Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iterate the child drawable of StateListDrawable in android

Tags:

android

In my current project,I need to iterate each of the child drawable in StateListDrawable.

In the source code of the StateListDrawable.java, I find a method:

public Drawable getStateDrawable(int index)

but the annotation of this method is @hide, which means I can not use it.

So, is there any alternative way to achieve this?

like image 500
Rakel He Avatar asked Oct 17 '12 01:10

Rakel He


1 Answers

This is a fairly old question now, so it's probably too late to help you, but for the sake of anyone else who finds it I was able to do it with this code:

final DrawableContainer parentDrawable = (DrawableContainer) drawable;
final DrawableContainerState containerState = (DrawableContainerState) parentDrawable.getConstantState();
final Drawable[] children = containerState.getChildren();
for (int i = containerState.getChildCount(); i > 0; --i) {
    doStuff(children[i - 1]);
}
like image 64
Squatting Bear Avatar answered Oct 11 '22 16:10

Squatting Bear