dcp3450
dcp3450

Reputation: 11187

In Android, how can I set the order of children in a FrameLayout as they're added?

I have a FrameLayout that will hold a set of ImageViews. I'm using FrameLayout because I actually want them to overlap. However, as I add items to the Layout I don't want them to just stack. I actually need to order them.

The image are pulled form storage and I have their correct order stored in my database so:

Image1 - layerNumber: 3
Image2 - layerNumber: 12
Image3 - layerNumber: 1
Image4 - layerNumber: 34
Image5 - layerNumber: 6
Image6 - layerNumber: 30

When I place them in the FrameLayout I'll need to them to in the Layout in the layerNumber order. Also, I'll be adding one or more at a time as I use the app. I may tab something that adds "Image50 - layerNumber: 10".

What I was hoping to do was use something like ImageView.setChildOrder(x) or something like that. Is there any way of doing this?

----- edit ----- per @fifarunnerr I did something like this:

productLayers.addView(productIV, Integer.parseInt(productItem.get(1)));

productItem.get(1) is just the layerNumber. For this example it's on layer 23.

I get the error: java.lang.IndexOutOfBoundsException: index=23 count=3

I understand the error: I'm trying to place a view in a layout that has 3 "spots" at the 23rd spot. As you can see in my numbers from earlier, the layerNumber won't always be in a 1..2..3..4... pattern.

Upvotes: 4

Views: 2682

Answers (2)

nKn
nKn

Reputation: 13761

Not sure if it's the way you want to do it, but you could simply declare your ImageViews wrapped into Relative/LinearLayouts, and setting each Layout an id, you could simply call relativeLayout1.bringToFront() on it.

Upvotes: 3

fifarunnerr
fifarunnerr

Reputation: 617

The z-order of children in the FrameLayout depends on the order in which the items are added, according to this thread.

So you could sort the layerNumber, and then add the childrens in the correct order, or you could use the addView(view, index) method.

Upvotes: 6

Related Questions