片段之间的滑动动画无法正确显示

时间:2019-05-02 21:02:16

标签: android android-fragments android-animation

首先,我想说的是我正在使用android.app.Fragment作为片段。左右滑动时,我可以看到片段上的值发生了变化,但是没有实际的滑动动画。相反,它只是在一瞬间相互更新,然后显示更新的/新的片段。

我的片段

public CardFlipActivity {
....
  public static class BackCardFragment extends Fragment {

        private static BackCardFragment clone(BackCardFragment backCardFragment){
            BackCardFragment clone = new BackCardFragment();
            Bundle args = new Bundle();
            args.putString("navigation_item", backCardFragment.getArguments().getString("navigation_item"));
            args.putString("card_type", backCardFragment.getArguments().getString("card_type"));
            clone.setArguments(args);
            return clone;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.back_card_fragment, container, false);
            view.setClickable(true);
            view.setFocusable(true);
            setUpGestures(view);
            return view;
        }

        private void setUpGestures(View view){

            final GestureDetector gesture = new GestureDetector(getActivity(), new GestureDetector.SimpleOnGestureListener() {

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                                       float velocityY) {
                    final int SWIPE_MIN_DISTANCE = 120;
                    final int SWIPE_MAX_OFF_PATH = 250;
                    final int SWIPE_THRESHOLD_VELOCITY = 200;
                    try {
                        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                            return false;
                        if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                            getFragmentManager()
                                    .beginTransaction()
                                    .setCustomAnimations(R.animator.enter_from_right, R.animator.exit_to_right,
                                            R.animator.enter_from_right, R.animator.exit_to_right)
                                    .replace(R.id.fragment_container, BackCardFragment.clone(BackCardFragment.this))
                                    .commit();
                        } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                            getFragmentManager()
                                    .beginTransaction()
                                    .setCustomAnimations(R.animator.enter_from_right, R.animator.exit_to_right,
                                            R.animator.enter_from_right, R.animator.exit_to_right)
                                    .replace(R.id.fragment_container, BackCardFragment.clone(BackCardFragment.this))
                                    .commit();
                        }
                    } catch (Exception e) {
                        Log.e("ERROR", "Something went wrong in onFling " + e.getMessage());
                    }
                    return super.onFling(e1, e2, velocityX, velocityY);
                }
            });

            view.setOnTouchListener((v, event) -> {
                return gesture.onTouchEvent(event);
            });
        }
    }
}

布局

back_card_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    android:gravity="bottom"
    android:id="@+id/backFragment"
    >

</LinearLayout>

card_flip_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.s.flashcard.CardFlipActivity">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

动画师

enter_from_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <objectAnimator
        android:fromXDelta="100%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="200" />
</set>

exit_to_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <objectAnimator
        android:fromXDelta="0%" android:toXDelta="100%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="200" />
</set>

我一直在看YouTube视频和其他人的帖子,但是对于我一生来说,我仍然看不到如何修复动画。

0 个答案:

没有答案