Comprehensive transport state management

Does anyone know what the appropriate mechanism is when creating a custom composite control to apply state changes from the container to all children? It seems to me that there should be an easy way to configure the ViewGroup to forward all state changes (pressed, enabled, etc.) to each child.

For example, if I create my own widget line by line:

 public class MyWidget extends RelativeLayout { private TextView mTitleView, mValueView; private ImageView mImageView; public ValueButton(Context context) { this(context, null); } public ValueButton(Context context, AttributeSet attrs) { super(context, attrs); //View setup, etc. } } 

In many cases, there are paintable lists or colors attached to child elements that I want to switch when the changes apply to the overall widget as a whole. What do I need to add to a similar widget so that, for example, when I call MyWidget.setEnabled() or when MyWidget clicked, do these state changes filter the hierarchy?

Thanks!

+4
source share
2 answers

Add android:duplicateParentState="true" to each child (possibly iterating through the children using getChildAt(int index) )

http://developer.android.com/reference/android/view/View.html#attr_android:duplicateParentState

Edit : you will need to install it in xml

Note: in the current implementation, setting this property to true after the view has been added to the ViewGroup has no effect at all. This property should always be used from XML or set to true before adding this view to the ViewGroup.

+3
source

I'm not sure that I will be very useful here, but this is an interesting problem for me, because I think that I would like to do something completely similar.

In terms of cascading state information up to child views, can one possible approach be to only contain this state information in the parent ViewGroup and use getParent () in child views to access this information?

0
source

All Articles