Question

I have a FrameLayout (within a RelativeLayout) to/from which I add/remove Fragments by means of the FragmentTransaction class. My FrameLayout is originally left-aligned with its parent and sometimes offset horizontally by means of the FrameLayout.offsetLeftAndRight(int offset) method.

I'm finding that when my FrameLayout is offset and I commit a fragment transaction to add a Fragment to it (i.e. FragmentTransaction.add(R.id.myFrameLayout, myNewFragment).commit()), my FrameLayout jumps back to its original location, ignoring its offset.

Anyone know why this is happening and how I can avoid my FrameLayout re-positioning itself when I add a Fragment to it?

Note: I'm using the offsetLeftAndRight(int offset) method rather than the setTranslationX(float translationX) or other methods because I need what I'm doing to work pre-Honeycomb.

Was it helpful?

Solution

Solved by keeping tracking of the offset position and overriding my View's onLayout(boolean changed, int left, int top, int right, int bottom) method to offset my View appropriately whenever this method is called, as follows:

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
  super.onLayout(changed, left, top, right, bottom);

  if (changed)
  {
    offsetLeftAndRight(myHorizontalOffset);
    offsetTopAndBottom(myVerticalOffset);
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top