Проведите между тем же типом фрагмента - PullRequest
1 голос
/ 06 марта 2019

Я пытаюсь создать смахивающую анимацию для одного типа фрагмента, FrontCardFragment, чтобы во время показа этого фрагмента, при пролистывании влево или вправо, он отображал анимацию и создавал другой FrontCardFragment, но с другими атрибутами.

На данный момент, когда я провожу пальцем влево или вправо, я вижу, что атрибут действительно изменяется, но реальной анимации нет.Я подозреваю, что я неправильно вызываю анимации, которые находятся в моей папке animator, в дополнение к чему-то еще, чего я не знаю.

Вот что у меня есть

public static class FrontCardFragment extends Fragment {

    private static FrontCardFragment clone(FrontCardFragment frontCardFragment){
        FrontCardFragment clone = new FrontCardFragment();
        //copy common values from bundle...
        return clone;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

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

    public 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) {
                        Log.i("INFO", "*************Right to Left");
                        incrementCurrentIndex();
                        getFragmentManager()
                                .beginTransaction()
                                .setCustomAnimations(R.animator.anim_slide_in_right, R.animator.anim_slide_out_right)
                                .replace(R.id.container, FrontCardFragment.clone(FrontCardFragment.this))
                                .addToBackStack(null)
                                .commit();
                    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                        Log.i("INFO", "************Left to Right");
                        decrementCurrentIndex();
                        getFragmentManager()
                                .beginTransaction()
                                .setCustomAnimations(R.animator.anim_slide_out_right, R.animator.anim_slide_in_right)
                                .replace(R.id.container, FrontCardFragment.clone(FrontCardFragment.this))
                                .addToBackStack(null)
                                .commit();

                    }
                } catch (Exception e) {
                }
                return super.onFling(e1, e2, velocityX, velocityY);
            }
        });

        view.setOnTouchListener((v, event) -> {
            return gesture.onTouchEvent(event);
        });
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if(currentIndex != -1 && documents.get(currentIndex) != null) {
            ((TextView) getView().findViewById(R.id.frontText))
                    .setText(documents.get(currentIndex).getString(getArguments().getString("card_type")));
        } else {
            ((TextView) getView().findViewById(R.id.frontText))
                    .setText("DB is empty for " + getArguments().getString("navigation_item") + " cards");
        }
    }
}

и мои анимации:

anim_slide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Sliding from right (100) to left (0)-->
    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="600"
        android:fromXDelta="100%"
        android:toXDelta="0%" >
    </objectAnimator>
</set>

anim_slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Sliding from left (-100) to right (0)-->
    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="600"
        android:fromXDelta="-100%"
        android:toXDelta="0%" >
    </objectAnimator>
</set>

anim_slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Sliding from right (0) to left (-100)-->
    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="600"
        android:fromXDelta="0%"
        android:toXDelta="-100%" >
    </objectAnimator>
</set>

anim_slide_out_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Sliding from left (0) to right (100)-->
    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="600"
        android:fromXDelta="0%"
        android:toXDelta="100%" >
    </objectAnimator>
</set>
...