如何在Android中启动带有转换的DialogFragment?

时间:2016-05-27 11:33:15

标签: android material-design transition

以下是我的情景。

我有一个Activity MainActivity,其中有一个FAB。当用户点击FAB时,我会打开一个全屏DialogFragment。我想用一些过渡打开DialogFragment

这是我到目前为止尝试过的代码。

//MainActivity.java

 final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
 fab.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {

    ReviewDialog reviewDialog = ReviewDialog.newInstance();
    Slide slide = new Slide();
    slide.setSlideEdge(Gravity.LEFT);
    slide.setDuration(1000);
    reviewDialog.setEnterTransition(slide);
    Bundle bundle =ActivityOptions.makeSceneTransitionAnimation(ScrollingActivity.this)
        .toBundle();
    reviewDialog.setArguments(bundle);
    reviewDialog.show(getSupportFragmentManager(),"review");
  }
});

这是DialogFragment ReviewDialog的代码。

   //ReviewDialog.java
    public class ReviewDialog extends DialogFragment {
  static ReviewDialog newInstance() {
    ReviewDialog f = new ReviewDialog();

    // Supply num input as an argument.
    Bundle args = new Bundle();
    f.setArguments(args);
    return f;
  }

  @Override
  public void onActivityCreated(Bundle arg0) {
    super.onActivityCreated(arg0);
    Slide slide = new Slide();
    slide.setSlideEdge(Gravity.LEFT);
    slide.setDuration(1000);
    getDialog().getWindow().setEnterTransition(slide);
    getDialog().getWindow().setExitTransition(slide);
    getDialog().getWindow().setReenterTransition(slide);}

  @Override
  public void onCreate(Bundle bundle){
    super.onCreate(bundle);
    setStyle(DialogFragment.STYLE_NORMAL,R.style.DialogTheme);
  }

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

}

此外,我在AndroidManifest.xml

中设置了以下属性
<item name="android:windowContentTransitions">true</item>

问题是当ReviewDialog启动时,它不会显示任何转换。 我能够显示不同活动之间的转换,但很难显示活动和片段之间的转换。 如何在启动DialogFragment时显示转换?

1 个答案:

答案 0 :(得分:-1)

使用此库:https://github.com/lgvalle/Material-Animations

共享元素转换与Fragments的工作方式非常类似于活动。

a)启用窗口内容转换

<强>值/ styles.xml

<style name="MaterialAnimations" parent="@style/Theme.AppCompat.Light.NoActionBar">
    ...
    <item name="android:windowContentTransitions">true</item>
    ...
</style>

b)定义公共转换名称

<强>布局/ fragment_a.xml

<ImageView
        android:id="@+id/small_blue_icon"
        style="@style/MaterialAnimations.Icon.Small"
        android:src="@drawable/circle"
        android:transitionName="@string/blue_name" />

<强>布局/ fragment_b.xml

<ImageView
        android:id="@+id/big_blue_icon"
        style="@style/MaterialAnimations.Icon.Big"
        android:src="@drawable/circle"
        android:transitionName="@string/blue_name" />

c)使用共享元素

启动片段
FragmentB fragmentB = FragmentB.newInstance(sample);

// Defines enter transition for all fragment views
Slide slideTransition = new Slide(Gravity.RIGHT);
slideTransition.setDuration(1000);
sharedElementFragment2.setEnterTransition(slideTransition);

// Defines enter transition only for shared element
ChangeBounds changeBoundsTransition = TransitionInflater.from(this).inflateTransition(R.transition.change_bounds);
fragmentB.setSharedElementEnterTransition(changeBoundsTransition);

getFragmentManager().beginTransaction()
        .replace(R.id.content, fragmentB)
        .addSharedElement(blueView, getString(R.string.blue_name))
        .commit();

这是最终结果:

enter image description here