安卓FragmentTransaction:如何产生的叠加幻灯片,将现有Fragement到左幻灯片、FragmentTransaction、Fragement

2023-09-06 00:41:18 作者:|▍你已成为了我的回忆〞

我试图做到以下几点。创建一个新的Fragement B(菜单),已经显示片段A左侧从右侧滑动,和我想移动(无隐藏或更换!)。

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.

我从片段B右侧交易,但片段A并没有改变他的位置都没有。好像,我的 FragmentManager 不知道片段A存在(片段A是不是动态添加,它在XML中定义)。

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

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

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();

片段A存在于 FragementManager ,我可以将其隐藏,但是这不是我的目标。

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?

一些注意事项:

在我使用的支持Libaries(android.support.v4.app.FragmentManager ..),但测试的API 17 正如前面提到的,我没有申报碎裂专门的 FragmentManager (尝试过,但它说片段已存在) 在片段A是(你可以在XML中看到)从类谷歌地图 SupportMapFragment 动画XML的工作,这就是为什么我没有将它张贴 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

推荐答案

解决方法:

创建相对布局重叠的片段:

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>

和简单的叫 .translationX(-1 * mScreenWidth)(mScreenWidth是我的屏幕宽度减去你想的差距)。

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);
            }