I have a DialogFragment whose only child is a StackView. The dialog is launched when a button is clicked in the parent fragment.
I tried to add animations to the fragment via getWindow().setWindowAnimations() but for some reason it was not working.
The approach that I took is to animate the Window's decorView:
public class TrailerDialogFragment extends DialogFragment {
private static final String VIDEOS_EXTRA = "videos extra";
private List<Video> mVideos = new ArrayList<>();
@BindView(R.id.stack_view)
StackView mStackView;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Video> videos = getArguments().getParcelableArrayList(VIDEOS_EXTRA);
if (videos != null) {mVideos.addAll(videos);}
}
public static TrailerDialogFragment newInstance(List<Video> videos) {
Bundle bundle = new Bundle();
ArrayList<Video> arrayList = new ArrayList<>();
arrayList.addAll(videos);
bundle.putParcelableArrayList(VIDEOS_EXTRA, arrayList);
TrailerDialogFragment fragment = new TrailerDialogFragment();
fragment.setArguments(bundle);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog_stackview, container, false);
ButterKnife.bind(this, rootView);
mStackView.setAdapter(new VideoAdapter(mVideos));
return rootView;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
return dialog;
}
@Override
public void onStart() {
super.onStart();
final View decorView = getDialog()
.getWindow()
.getDecorView();
ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(decorView,
PropertyValuesHolder.ofFloat("scaleX", 0.0f, 1.0f),
PropertyValuesHolder.ofFloat("scaleY", 0.0f, 1.0f),
PropertyValuesHolder.ofFloat("alpha", 0.0f, 1.0f));
scaleDown.setDuration(500);
scaleDown.start();
}
@Override
public void onCancel(DialogInterface dialog) {
final View decorView = getDialog()
.getWindow()
.getDecorView();
ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(decorView,
PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0.0f),
PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0.0f),
PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
scaleDown.setDuration(500);
scaleDown.start();
super.onCancel(dialog);
}
private static class VideoAdapter extends BaseAdapter {
List<Video> mVideos = new ArrayList<>();
public VideoAdapter(List<Video> videos) {
mVideos.addAll(videos);
}
@Override
public int getCount() {
return mVideos.size();
}
@Override
public Object getItem(int i) {
return mVideos.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View resultView;
if (view == null) {
resultView = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.dummy_view, viewGroup, false);
} else {
resultView = view;
}
TextView tv = (TextView) resultView.findViewById(R.id.trailer_title);
ImageView image = (ImageView) resultView.findViewById(R.id.trailer_image);
tv.setText(mVideos.get(i).name());
ViewUtil.loadThumbnail(mVideos.get(i).key(), resultView.getContext(), image);
return resultView;
}
}
}
When I click outside of dialog fragment, the onCancel callback is triggered, but for some reason the animation doesn't play. The DialogFragment simply dissappears.
Do you know why this could happen?