1

I have a problem changing the Z-order of views inside a scrollView.

I hope I'll explain my use case correctly.

the use case is the following:

I have scrollView SV that has a linear layout LL.

inside the LL I add several views. The views are RelativeLayouts RL with 2 imageviews (IMG1 and IMG2)inside. IMG1 is visible IMG2 is gone.

When I trigger a certain action, I animate the imageView IMG2 that was gone so it slides to the right. It slides being at the back of IMG1 and only showing the part where it exceeds IMG1's width. This is ok and is what I want.

I have everything working except one thing: I want IMG2 to be at the back of IMG1 (as it is right now) but at the front of all other RL's inside the Scroll View.

I think my problem can be put down to the following: How can I change the Z-Order of the views inside a scroll view? because if I can z-order the RL's inside the scrollView (bringing to front or sending to back as I want) this use case will work.

From my tests I see that the z-order of the views is the same of the order i add the views to the scrollview, but this ruins my case as i need to bring to front or send to back the views as I want.

I tried every method of bringToFront and bringChildToFront and nothing works.

is there a way of doing this?

Nighthawk
  • 125
  • 1
  • 9

1 Answers1

1

I haven't tried it, but I'd suggest using removeView(v) followed by addView(v, index) to remove the child view, and re-insert it at the desired location in the order stack. This is probably an extremely heavyweight way to solve the problem, but it may work.

mportuesisf
  • 5,587
  • 2
  • 33
  • 26
  • Thank you for your answer. Already tried that but didn't work. – Nighthawk Jan 19 '12 at 17:49
  • Views that are GONE aren't counted as part of the layout. Perhaps that also extends to child ordering. Have you tried making the inactive views INVISIBLE rather than GONE? – mportuesisf Jan 19 '12 at 18:07
  • good call but still doesn't work, even if I remove and re-add the view with the IMG2 being invisible instead of gone. – Nighthawk Jan 19 '12 at 18:16
  • Hmm... I'm out of bright ideas. It won't solve your problem, but the next thing I'd do is to try it again without the LL embedded inside the ScrollView, to see if the ScrollView is part of the problem. Have you checked out http://stackoverflow.com/questions/2599837/linearlayout-not-expanding-inside-a-scrollview - they suggest using android:fillViewport=true on the ScrollView. – mportuesisf Jan 19 '12 at 18:26
  • You might want to list all of the things you've tried, so the next person won't have to :) – mportuesisf Jan 19 '12 at 18:54
  • LinearLayout's z-order is based on the index of its children. To achieve what you want you should write a custom layout (note that you can override the children drawing order when doing so.) – Romain Guy Jan 19 '12 at 18:56
  • Thank you Romain, I will try to do that. Is there any example that I could follow? – Nighthawk Jan 20 '12 at 09:18
  • Romain, what you're saying is that I can change the drawing order of the views keeping the same appearance order? doesn't the LinearLayout use the order of appearence for the z-order? – Nighthawk Jan 20 '12 at 15:32