Android FragmentTransaction: How to generate an ov

2019-03-21 14:31发布

问题:

I'm trying to do the following. Create a new Fragement B (Menu), slide it in from the right, and i want to move (no hide or replace!) the already shown Fragment A to the left.

I got the Transaction from Fragment B right, but Fragment A doesn't change his position at all. Seems like, my FragmentManager doesn't know that Fragment A exists (Fragment A is not added dynamically, it's defined in XML).

  • main_screen_layout-xml

    <RelativeLayout //...
        android:id="@+id/main_screen"
        tools:context=".MainActivity" 
    
      <fragment
        android:id="@+id/map_screen"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        class="com.google.android.gms.maps.SupportMapFragment" >
      </fragment>
    
  • FragmentTransaction

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();            
    ft.add(R.id.main_screen, new MenuFragment(), MENU_FRAGMENT); //adding Fragment B
    ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left_cut);       
    ft.addToBackStack(null);
    ft.commit();
    

Fragment A exists in the FragementManager and I can hide it, but that's not my goal

Fragment fragmentA = fragmentManager.findFragmentById(R.id.map_screen);
ft.hide(fragmentA); //hide FragmentA

What am I doing wrong?

Some Notes:

  • I'm using the Support Libaries (android.support.v4.app.FragmentManager..), but testing on API 17
  • As mentioned before, I didn't declare FragmentA specifically to the FragmentManager (tried so, but it says Fragment already exists)
  • Fragment A is (as you can see in XML) a google map from class SupportMapFragment
  • The animation XML is working, that's why I didn't post it

回答1:

I'm not sure if this is exactly what you are looking for, but check out this Sliding Menu library... it allows one to slide in a fragment from the left or right while shifting the center content fragment.



回答2:

Solution:

Create Relative Layout with overlapping fragments:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_screen"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/back_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <include layout="@layout/menu_fragment" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/front_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <fragment
            android:id="@+id/map_screen"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@+id/map_screen_bottom_bar"
            class="com.google.android.gms.maps.SupportMapFragment" >
        </fragment>

and simply call .translationX(-1 * mScreenWidth) (mScreenWidth is my ScreenWidth minus the gap you want).

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
                ((RelativeLayout) findViewById(R.id.front_frame)).animate()
                    .translationX(-1 * mScreenWidth);
            } else {
                TranslateAnimation slideOut = new TranslateAnimation(0, -1
                    * mScreenWidth, 0, 0);
                slideOut.setDuration(getResources().getInteger(
                    R.integer.animation_transistion_length_short));
                slideOut.setFillEnabled(true);

                slideOut.setAnimationListener(new AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {}


                    @Override
                    public void onAnimationRepeat(Animation animation) {}


                    @Override
                    public void onAnimationEnd(Animation animation) {
                        RelativeLayout mView = (RelativeLayout) findViewById(R.id.front_frame);
                        int[] pos = { mView.getLeft() - mScreenWidth,
                            mView.getTop(), mView.getRight(),
                            mView.getBottom() };
                        mView.layout(pos[0], pos[1], pos[2], pos[3]);
                    }
                });

                ((RelativeLayout) findViewById(R.id.front_frame))
                    .startAnimation(slideOut);
            }