Reputation: 4405
I have a custom view implemented in Java that is a vertical layout with 4 child views.
I would like to be able to pass a configuration for each child view via a method in the custom view.
Let's assume I just want to pass a different color id per view e.g. for child 1 use R.color.white
and for child 2 use R.color.red
etc
How could I pass this information to the custom view? The following would work:
public void configure(int color1, int color2, int color3, int color4)
but the problem with that is that I would prefer a way that the caller of the method indicates which color is for which child (1,2,3,4) instead of passing the colors fixed in the method call
Upvotes: 0
Views: 52
Reputation: 54194
If you want to be able to allow callers to configure individual children, I recommend exposing individual methods for each child:
public void configureChild1(int color)
public void configureChild2(int color)
...
If you want just a single "dynamic" method, you will have to accept something like a Map
from position to color, or a vararg Pair
of position and color:
public void configure(Map<Integer, Integer> positionsToColors)
public void configure(Pair<Integer, Integer> ... positionAndColors)
The downside for these is that there's no compile-time way of knowing that the passed-in positions are valid.
Upvotes: 1