Different z-position for ListView items
Asked Answered
M

1

10

Is it possible to make items in a ListView have different z-position/index?

I want one item in my ListView to always be on top of other views in my layout.

See my illustration here:

enter image description here

The red bar is another view outside the ListView (in my case a vertical seekbar).

I have tried calling View.bringToFront() on a certain item but that didn't work at all.

Any ideas?

Moonshot answered 29/4, 2013 at 18:38 Comment(2)
Just like any value you can change the z-index so that the object moves forwards or backwards in its page position as the user interacts with the page.Kitts
@Kitts changing the Z-index of the view will just change the position relative to its sibling views. In a ListView all items in the list are siblings, but changing the Z-index will have no effect since the list items do not overlap. Changing the Z-index of one item in the list will not make it jump forward to be in front of another view that isn't a sibling of the views in the ListView.Nelrsa
S
1

Here is how I would go about achieving the effect you desire:

Each item in the list should have a red bar in it that is the height of the item. That way all the red bars line up making it look like there is just one red bar when in fact there is one per item.

If you are using a FrameLayout, the order of the children determines the drawing order, for example the 3rd child of a FrameLayout will appear on top of the 1st and 2nd child.

Then at runtime you can arrange the red bar above or below the contents of a each item. In the bindView method of your adapter you can use the bringToFront method to bring the red bar to the front for that view. Don't forget that views are recycled in the ListView so you will need to move the red bar to correct z position every time.

Here are is a very stripped down example XML file for an item in the view, let me know if you need more information.

item_red_bar_bottom.xml:

<FrameLayout ...>
   <ImageView .../> <!-- Red bar on bottom -->
   <FrameLayout ...>
       <!-- Normal item contents go here -->
   </FrameLayout>
</FrameLayout>
Sodomite answered 2/6, 2013 at 2:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.