我有一个DialogFragment,其唯一的子节点是StackView。在父片段中单击按钮时,将启动该对话框。
我尝试通过getWindow()。setWindowAnimations()向片段添加动画但由于某种原因它无法正常工作。
我采用的方法是为Window的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;
}
}
}
当我单击对话框片段外部时,会触发onCancel回调,但由于某种原因,动画无法播放。 DialogFragment只是消失了。
你知道为什么会这样吗?