Android FragmentTransaction: How to generate an overlay slide in and move existing Fragement to left

前端 未结 2 1634
囚心锁ツ
囚心锁ツ 2021-02-10 15:23

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.

相关标签:
2条回答
  • 2021-02-10 16:24

    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);
                }
    
    0 讨论(0)
  • 2021-02-10 16:27

    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.

    0 讨论(0)
提交回复
热议问题