如何在单击“onbackpressed”到片段时添加动画

How to add animation on clicking `onbackpressed` to fragment

有一个Fragment,里面有一个按钮,按下一个Fragment打开有动画,但是按回按钮时,就是onbackpressed , 之前的 Fragment 被返回,但没有动画。如何在点击时添加动画 onbackpressed。据我了解,您需要以某种方式添加 onbackpressed 并为其分配动画。那么如何把onbackpressed加到Fragment呢?

?

Fragment 我们从那里经过

public class FragmentAttraction extends Fragment {

    FragmentGyro fragmentGyro;
    BlankFragment blankFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_attraction, container, false);
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {

        fragmentGyro = new FragmentGyro();
        blankFragment= new BlankFragment();

        CardView gyro = requireView().findViewById(R.id.Gyro_);


        gyro.setOnClickListener(v -> {
            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            ft.setCustomAnimations(R.anim.transition_enter, R.anim.transition_exit);
            ft.replace(R.id.fragment, blankFragment, "detailFragment");
            ft.addToBackStack(null);
            ft.commit();
        });
    }
}

我们去的Fragment

public class FragmentGyro extends Fragment {

    private final String[] imageUrls = new String[]{
            "https://cdn.pixabay.com/photo/2016/11/11/23/34/cat-1817970_960_720.jpg",
            "https://cdn.pixabay.com/photo/2017/12/21/12/26/glowworm-3031704_960_720.jpg",
            "https://cdn.pixabay.com/photo/2017/12/24/09/09/road-3036620_960_720.jpg",
            "https://cdn.pixabay.com/photo/2017/11/07/00/07/fantasy-2925250_960_720.jpg",
            "https://cdn.pixabay.com/photo/2017/10/10/15/28/butterfly-2837589_960_720.jpg"
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_gyro, container, false);

    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        ViewPager viewPager = requireView().findViewById(R.id.view);
        ViewPagerAdapter adapter = new ViewPagerAdapter(requireActivity(), imageUrls);
        viewPager.setAdapter(adapter);

        TextView text = requireView().findViewById(R.id.text_o);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @SuppressLint("DefaultLocale")
            @Override
            public void onPageSelected(int position)
            {
                text.setText(String.format("%d/%d", position, 4 ));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}

setCustomAnimations()有两个版本:

  • 您正在使用的那个只需要 enterexit 动画
  • four arguments version 也采用 popEnterpopExit 动画 - 这些是片段弹出时 运行 的动画(即,当您点击后退按钮)。
ft.setCustomAnimations(R.anim.transition_enter, R.anim.transition_exit,
    R.anim.transition_enter, R.anim.transition_exit);