在相同的片段类型之间滑动

时间:2019-03-05 22:24:18

标签: android swipe

我正在尝试为一个片段类型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>

0 个答案:

没有答案