1

我想达到特定的片段转换行为,正如我在问题中提到的那样,我希望我的片段从右侧出现并以相同的方向消失,回到它开始的点。这些是我的 xml 动画:

right_to_left.xml :

<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
    android:duration="1000"
    android:fromXDelta="100%"
    android:fromYDelta="0%"
    android:toXDelta="0%"
    android:toYDelta="0%" />

left_to_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:duration="1000"
    android:fromXDelta="0%"
    android:fromYDelta="0%"
    android:toXDelta="-100%"
    android:toYDelta="0%" />

以下是我尝试实现片段动画的不同方式:

当片段出现时:

  productHolder.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putSerializable(Utils.productDetailsKey, productPojo);
                ProductDetailsFragment productDetailsFragment = new ProductDetailsFragment();
                productDetailsFragment.setArguments(bundle);
                FragmentManager fragmentManager = activity.getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.setCustomAnimations(R.anim.product_animation_enter, R.anim.product_animation_enter);
                transaction.add(R.id.newContainer, productDetailsFragment);
                transaction.commit();
                MainActivity.transparent.setBackgroundColor(ContextCompat.getColor(context, R.color.blackTransparent));


            }
        });

当片段消失时方法1:

 fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.setCustomAnimations(R.anim.product_animation_exit, R.anim.product_animation_exit);
                transaction.remove(ProductDetailsFragment.this);
                transaction.commitAllowingStateLoss();
                MainActivity.transparent.setBackgroundColor(0x00000000);


            }
        });

当片段消失时方法2:

fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.product_animation_exit);
                    animation.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
                    animation.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            try {
                                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                                transaction.remove(ProductDetailsFragment.this);
                                transaction.commitAllowingStateLoss();
                                MainActivity.transparent.setBackgroundColor(0x00000000);

                            } catch (Exception e) {
                            }

                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });

                    getView().startAnimation(animation);
                }
            });

不幸的是,这些都没有像我预期的那样工作,希望这张图片可以解释更多我想要的东西:图片 提前谢谢你。

4

1 回答 1

1

我建议,首先你为问题的第一部分制定一个可行的解决方案,即在片段之间切换,然后添加第二部分,即动画。

1. 切换片段

在您的 XML 布局文件中,您取出属于片段的所有内容并添加一个Framelayout.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

在您的代码中,您会获得对FrameLayout类似内容的引用:

int containerId = R.id.fragment_container;

然后你有两种方法,一种用于添加第一个片段,另一种用于在两者之间来回切换。

add 方法是这样的:

void addFrag(int containerId, Fragment firstFragment){
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(containerId, firstFragment);
        transaction.commit();
}

替换方法是这样的:

void replaceFrag(int containerId, Fragment newFragment){
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(containerId, newFragment);
        transaction.commit();
}

只要您传递了正确的片段,您就可以使用此方法将您的任何片段替换为另一个片段。编辑您也可以replaceFrag使用该方法,即使是第一次将片段添加到容器中。编辑结束

2.动画

这真的很容易。您不需要制作自己的 XML,因为滑动动画是内置于 Android 中的(因此请使用我在此处提供的确切 ID)。您所要做的就是在添加或替换片段之前添加一行代码

transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);

编辑由于这些内置动画从 50% 开始,但是,在我看来,它们看起来不太好。您可以简单地使用您的 XML:

transaction.setCustomAnimations(R.anim.left_to_right, R.anim.right_to_left);

编辑结束

于 2017-10-02T01:56:06.603 回答