如何设置此片段转换的动画

时间:2018-05-20 06:40:44

标签: java android android-fragments fragment

recyclerView.addOnItemTouchListener(new GalleryAdapter.RecyclerTouchListener(getApplicationContext(), recyclerView, new GalleryAdapter.ClickListener() {

        @Override
        public void onClick(View view, int position) {

            Bundle bundle = new Bundle();
            bundle.putSerializable("images", images);
            bundle.putInt("position", position);
            SlideshowDialogFragment newFragment = SlideshowDialogFragment.newInstance();

            FragmentTransaction ft = getSupportFragmentManager()
                    .beginTransaction();
            newFragment.setArguments(bundle);
            newFragment.show(ft, "slideshow");
        }

        @Override
        public void onLongClick(View view, int position) {
            Toast.makeText(MainActivity.this, "long click" , 
                 Toast.LENGTH_SHORT).show();

        }
    }
));

1 个答案:

答案 0 :(得分:0)

你应该参考我用过的方法 -

@Override
public void switchFragment(@Nullable android.app.Fragment targetFragment, @Nullable boolean addToBackStack, Bundle bundle, @Nullable String fragmentTag) {
    android.app.FragmentManager fragmentManager = getFragmentManager();
    android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    if (addToBackStack) {
        fragmentTransaction.addToBackStack(fragmentTag);
    }
    if (bundle != null) {
        targetFragment.setArguments(bundle);
    }
    fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
    fragmentTransaction.replace(R.id.container, targetFragment);
    fragmentTransaction.commit();

}

并将动画文件放在动画文件夹中 -

enter_from_left.xml

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

enter_from_right.xml

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

exit_to_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="0%" android:toXDelta="-100%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="300"/>
</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">
    <translate
        android:fromXDelta="0%" android:toXDelta="100%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="300" />
</set>