首先要在res/anim文件夹下创建两个xml文件(一个弹出动画 一个下滑消失动画)
弹出动画:
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromYDelta="100%p"
android:fillAfter="true"
android:toYDelta="0%p"/>
<!-- duration:属性为动画持续时间,时间以毫秒为单位。-->
<!-- fromXDelta:属性为动画起始时,X坐标上的位置。-->
<!-- fromYDelta:属性为动画起始时,Y坐标上的位置。-->
<!-- toXDelta:属性为动画结束时,X坐标上的位置。-->
<!-- toYDelta:属性为动画结束时,Y坐标上的位置。-->
下滑消失:
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromYDelta="0%p"
android:toYDelta="100%p"/>
<!-- duration:属性为动画持续时间,时间以毫秒为单位。-->
<!-- fromXDelta:属性为动画起始时,X坐标上的位置。-->
<!-- fromYDelta:属性为动画起始时,Y坐标上的位置。-->
<!-- toXDelta:属性为动画结束时,X坐标上的位置。-->
<!-- toYDelta:属性为动画结束时,Y坐标上的位置。-->
以评论按钮为例子
在add前使用setCustomAnimations(进入时动画,退出时动画)
注意 该处参数是根据setCustomAnimations方法后的方法(如add,hide等)来进行选择执行哪一个参数的动画 因此 在评论窗返回的时候也要再设置一个退回动画 (如果不设置的话 哪怕设置点击事件里hide该fragment 评论页面仍然会存在不消失 暂时推测为动画的原因)
setCustomAnimations(R.anim.comment_up_animation, R.anim.comment_down_animation) .add()
评论的点击事件中:
@Override
public void setOnCommentClickListener(ArrayList<CommentData> commentDataArrayList) {
Fragment feedCommentFragment = getFragmentManager().findFragmentByTag("feedCommentFragment");
if (feedCommentFragment == null) {
feedCommentFragment = FeedCommentFragment.newInstance(commentDataArrayList);
getActivity().getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.comment_up_animation, R.anim.comment_down_animation)
.add(R.id.linear_comment, feedCommentFragment, "feedCommentFragment")
.addToBackStack(null).commitAllowingStateLoss();
} else {
getActivity().getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.comment_up_animation, R.anim.comment_down_animation)
.show(feedCommentFragment)
.addToBackStack(null).commitAllowingStateLoss();
}
}
返回的点击事件:
@Override
public void onClick(View v) {
Fragment feedCommentFragment = getFragmentManager().findFragmentByTag("feedCommentFragment");
getActivity().getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.comment_down_animation,R.anim.comment_down_animation)
.hide(feedCommentFragment).commitAllowingStateLoss();
}