繁体   English   中英

片段过渡从右到左出现,从左到右消失(回到起点)

[英]Fragment transition appears from right to left, disappears from left to right (back to the point it starts from)

我想要达到特定的片段过渡行为,正如我在问题中提到的那样,我希望片段从右边出现,并以相同的方向消失,直到回到起点为止。这些是我的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);
                }
            });

不幸的是,这些工作都没有我期望的那样,愿这张照片可以解释我想要的更多内容: image提前谢谢。

我建议,首先,为问题的第一部分提供有效的解决方案,即在片段之间切换,然后添加第二部分,即动画。

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

replace方法如下:

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

编辑结束

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM