1

I have an special use case where I have an an custom FrameLayout which contains 2 custom views. I will call it MyWrapperFL. I have another custom view, which is outside this custom FrameLayout. All of this is wrapped inside an basic FrameLayout. So I have the following:

CustomView 1
MyWrapperFL
  CustomView 2
  CustomView 3

Now I want to change the order of the views like this:

CustomView 3 (top most)
CustomView 1
CustomView 2 (bottom most)

I've tried using the following utility method I've created:

/**
   * Reorder the position of the views on their Z axis.
   * <br/>
   * The order of the views is important. The first in the list will be the top most view while the last in the list
   * will be the bottom most view.
   *
   * @param views
   *     The list of views which to reorder. Can be {@code null}.
   */
  private void updateViewsOrder(@Nullable View... views) {
    if (null == views) {
      // No views specified for reorder - exit early
      return;
    }
    List<View> viewList = Arrays.asList(views);

    int translationZ = viewList.size();
    for (View view : viewList) {
      if (null != view) {
        view.bringToFront();
        view.getParent().requestLayout();
        view.invalidate();
      }
    }
  }

But result is not the desired one. Instead of the expected result I get the following:

CustomView 1 (top most)
CustomView 3
CustomView 2 (bottom most)

I'm guessing this is because they have different parents. Can anyone help me with this? Is there a way to do it?

Ionut Negru
  • 6,186
  • 4
  • 48
  • 78
  • You can use anyview.bringToFront() method for bring view to front. – Bharat Beladiya Oct 11 '17 at 06:27
  • I have specified the issue in my question! `bringToFront()` does not work for my use case. – Ionut Negru Oct 11 '17 at 06:30
  • `bringToFront` does change order of children of one `ViewGroup`. So if `CustomView 1` is outside this `ViewGroup` then you need manually move view from one `ViewGroup` to another. – eleven Oct 11 '17 at 09:44
  • @eleven, thank you for your response. I was wondering if there is another easy way to do this. The `MyWrapperFL` is in a library module and it will be harder to refactor it for this specific use case. – Ionut Negru Oct 11 '17 at 18:26

0 Answers0