簡體   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