ViewPager中的片段与新片段之间的共享元素转换

时间:2017-07-24 15:34:23

标签: java android android-viewpager shared-element-transition

有没有办法使用共享元素从ViewPager中的片段转换为新的片段?

修改

我有一个MainActivity,其中ViewPager包含FragmentA。从那开始我想用共享元素转换打开FragmentB

我已经创建了一个示例项目,您可以在下面看到。如果我删除ViewPager并定期打开FragmentA,共享元素转换工作正常,但只要我将ViewPager添加到项目中,就不会出现转换。

这是MainActivity

的代码
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager mViewPager = (ViewPager)findViewById(R.id.viewpager);

    FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
        private int NUM_ITEMS = 1;
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new FragmentA();
                default:
                    return null;
             }
        }
        @Override
        public int getCount() {
            return NUM_ITEMS;
        }
    };
    mViewPager.setAdapter(adapter);
    }
}

MainActivity

的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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.gustafwennerstrom.testeb.MainActivity">
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:id="@+id/viewpager"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>
</RelativeLayout>

这是FragmentA

的代码
public class FragmentA extends Fragment {
    FrameLayout root;

    public FragmentA() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        root = (FrameLayout)inflater.inflate(R.layout.fragment_, null);
        ImageView image = (ImageView) root.findViewById(R.id.testImage);
        image.setOnClickListener(openFragment);
        return root;

    }

    private View.OnClickListener openFragment = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ImageView sharedImage = (ImageView)root.findViewById(R.id.testImage);
            getFragmentManager()
                .beginTransaction()
                .addSharedElement(sharedImage, ViewCompat.getTransitionName(sharedImage))
                .addToBackStack(getTag())
                .replace(android.R.id.content, new FragmentB())
                .commit();
    };
}

FragmentA的XML(FragmentB看起来非常相似)

<FrameLayout 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"
    android:id="@+id/fragment_a"
    android:background="@color/colorAccent"
    tools:context="com.example.gustafwennerstrom.testeb.FragmentA">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@drawable/yo"
        android:transitionName="myImage"
        android:id="@+id/testImage"/>
</FrameLayout>

这是fragmentB

public class FragmentB extends Fragment {
    public FragmentB() {}

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setSharedElementEnterTransition(TransitionInflater
            .from(getContext())
            .inflateTransition(android.R.transition.move)
            .setDuration(100));
         }
    }

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

    }
}

styles.xml我添加了这行代码:

<item name="android:windowContentTransitions">true</item>

0 个答案:

没有答案