androidandroid-layoutandroid-recyclerviewandroid-relativelayoutandroid-overlay

How to make all the elements of a RecyclerView, EXCEPT the first element, overlay the rest of the content of Activity?


I have an Activity with a RecyclerView and then a FrameLayout below it. Initially, the RecyclerView has one element which is a LinearLayout, and the rest of the elements will be added at runtime (those will also be LinearLayouts). The first picture below shows this initial setup.

Then at runtime (see the second picture below), some more LinearLayouts will be added to the RecyclerView(collectively shown as the green box).

Now the CHALLENGE is that the blue FrameLayout does not get overlapped or overlayed by the LinearLayout with orange border. But when the green LinearLayout is added to the RecyclerView, it should overlay everything (essentially the blue FrameLayout) under it.

One way would be to actually make the entire RecyclerView overlay everything else, and give the FrameLayout a paddingTop value equal to the height of the orange LinearLayout? But the problem with that is that (1)it seems hacky so it should probably be done when there is no other way (2) I don't know what the height of the orange LinearLayout (since it is wrap_content).

So how should I go about it?

enter image description here

enter image description here


Solution

  • Hoping I got things right, I would go with your proposed solution. It does not seem that hacky to me actually. You can always measure the first LinearLayout once it gets laid out and set the padding accordingly.

      getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() { 
                   getViewTreeObserver().removeOnGlobalLayoutListener(this);
                   measureAndSetPadding();
                }
      });