Прежде всего я хотел бы сказать, что я использую 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 и посты других людей, но до сих пор не могу понять, как исправить анимацию.