FrameLayout which is offset re-positions itself when a Fragment is added to it?
Domanda
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 thesetTranslationX(float translationX)
or other methods because I need what I'm doing to work pre-Honeycomb.
Soluzione
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);
}
}