问题描述
我有一个项目列表的活动,当你点击一个项目,我想播放控制该项目从屏幕底部向上滑动,变得可见。我定义的幻灯片和滑出动画组和他们的工作。我设置我的animationListener在我的活动,并开始了我在一个项目中动画的onClick幻灯片。我的问题是,我第一次运行该应用程序,当我点击一个项目,onClick的回调执行,但动画不会发生。我第二次点击,动画的幻灯片发生,而不是滑出。连续三次,它按预期工作。这里是我的动画集。
vm_slide_in.xml
< XML版本=1.0编码=UTF-8&GT?;
<设置的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:fillAfter =真正的>
<翻译
机器人:fromYDelta =800
机器人:toYDelta =0
机器人:时间=600/>
< /集>
vm_slide_out.xml
< XML版本=1.0编码=UTF-8&GT?;
<设置的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:fillAfter =真正的>
<翻译
机器人:fromYDelta =0
机器人:toYDelta =800
机器人:时间=600/>
< /集>
下面是我的活动布局
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
风格=@风格/ AppBG>
< RelativeLayout的
风格=@风格/ LogoBar
机器人:ID =@ + ID / logo_bar
机器人:layout_alignParentTop =真正的>
<包括布局=@布局/ logobar>< /包括>
< / RelativeLayout的>
< RelativeLayout的
风格=@风格/ TitleBar中
机器人:ID =@ + ID / title_bar
机器人:layout_below =@ + ID / logo_bar>
<包括布局=@布局/标题栏>< /包括>中
<按钮样式=@风格/ TitleBarButton
机器人:ID =@ + ID / vm_speaker_btn
机器人:文本=@字符串/ vm_speaker_btn_label
机器人:layout_alignParentLeft =真
机器人:layout_margin =4DP>
< /按钮>
<按钮样式=@风格/ TitleBarButton
机器人:ID =@ + ID / vm_edit_btn
机器人:文本=@字符串/ vm_edit_btn_label
机器人:layout_alignParentRight =真
机器人:layout_margin =4DP>
< /按钮>
< / RelativeLayout的>
<的ListView
机器人:ID =@ + ID / @机器人:ID /列表
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / title_bar/>
< RelativeLayout的
机器人:ID =@ + ID / vm_control_panel
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_above =@ + ID /页脚
机器人:能见度=水涨船高>
<包括布局=@布局/ vm_control/>
< / RelativeLayout的>
< RelativeLayout的
机器人:ID =@ + ID /页脚
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真正的>
<包括布局=@布局/任务栏/>
< / RelativeLayout的>
< / RelativeLayout的>
下面是针对包含控件的布局
< XML版本=1.0编码=UTF-8&GT?;
<合并的xmlns:机器人=http://schemas.android.com/apk/res/android>
< RelativeLayout的
机器人:ID =@ + ID / vm_control
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@色/ dark_grey>
<的TextView
机器人:ID =@ + ID / vm_ctl_branding
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentTop =真
机器人:layout_centerHorizontal =真
机器人:layout_marginTop =5DP
机器人:文本=@字符串/品牌
机器人:文字颜色=@色/白>
< / TextView的>
< TextView的风格=@风格/ TimeMark
机器人:ID =@ + ID / vm_timestamp
机器人:layout_toLeftOf =@ + ID / vm_progress
机器人:layout_below =@ + ID / vm_ctl_branding
机器人:layout_marginRight =3DP
机器人:文本=0:00>
< / TextView的>
<搜索栏
机器人:ID =@ + ID / vm_progress
机器人:layout_width =200dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / vm_ctl_branding
机器人:layout_centerHorizontal =真正的>
< /搜索栏>
< TextView的风格=@风格/ TimeMark
机器人:ID =@ + ID / vm_timeleft
机器人:layout_toRightOf =@ + ID / vm_progress
机器人:layout_below =@ + ID / vm_ctl_branding
机器人:layout_marginLeft =3DP
机器人:文本= - 0:00>
< / TextView的>
<的LinearLayout
机器人:ID =@ + ID / vm_action_button_layout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignLeft =@ + ID / vm_timestamp
机器人:layout_alignRight =@ + ID / vm_timeleft
机器人:方向=横向
机器人:layout_below =@ + ID / vm_progress>
< TextView的风格=@风格/ Vm_Action_Button
机器人:背景=@可绘制/ vm_action_btn_call
机器人:ID =@ + ID / vm_callback_btn
机器人:layout_marginRight =5DP
机器人:文本=@字符串/ vm_action_btn_callback>
< / TextView的>
< TextView的风格=@风格/ Vm_Action_Button
机器人:背景=@可绘制/ vm_action_btn_delete
机器人:ID =@ + ID / vm_delete_btn
机器人:layout_marginLeft =5DP
机器人:文本=@字符串/ vm_action_btn_delete>
< / TextView的>
< / LinearLayout中>
< / RelativeLayout的>
< /合并>
这是我的onCreate方法...
//处理列表项的选择
ListView的LV = getListView();
lv.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
最终的语音信箱vmItem =(语音)vmla.getItem(位置);
Toast.makeText(CTX,姓名:+ vmItem.getCallerName()+的位置:+位置,Toast.LENGTH_SHORT)
。显示();
vVm_controls.startAnimation(mSlideIn);
}
});
和我的动画回调...
@覆盖
公共无效onAnimationStart(动画动画){
vm_controls_in = vm_controls_in!;
如果(vm_controls_in){
vVm_controls.setVisibility(View.GONE);
} 其他 {
vVm_controls.setVisibility(View.VISIBLE);
// vVm_controls.bringToFront();
}
}
@覆盖
公共无效onAnimationEnd(动画动画){
如果(!vm_controls_in){
尝试 {
视频下载(1000);
vVm_controls.startAnimation(mSlideOut);
}赶上(InterruptedException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
}
@覆盖
公共无效onAnimationRepeat(动画动画){
// TODO自动生成方法存根
}
当你发现,这个问题是知名度。当视图最初设置为走在XML文件中,Android将不会呈现布局,直到能见度变为可见或不可见。如果你试图在尚未呈现又一个视图动画,动画将发生在没有布局视图。动画后完成,它会使其突然认为会出现无动画。它的工作原理随后的时间,因为该视图具有即使设置为离去的布局。
关键是能见度设置为不可见,而不是走在了XML文件,以便它呈现又隐藏。当动画发生的第一时间查看出现,预期将移动。
I have an Activity with a list of items and when you click on an item, I want playback controls for that item to slide up from the bottom of the screen and become visible. I've defined an animation set for the slide in and the slide out and they work. I've setup my animationListener in my activity and started my slide in animation onClick of an item. My problem is, the first time I run the app, when I click on an item, the onClick callback is executed, but the animation doesn't happen. Second time I click, the slide in animation happens, but not the slide out. Third and subsequent times, it works as expected. Here are my animation sets.
vm_slide_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<translate
android:fromYDelta="800"
android:toYDelta="0"
android:duration="600" />
</set>
vm_slide_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<translate
android:fromYDelta="0"
android:toYDelta="800"
android:duration="600" />
</set>
Here is my activity layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/AppBG">
<RelativeLayout
style="@style/LogoBar"
android:id="@+id/logo_bar"
android:layout_alignParentTop="true">
<include layout="@layout/logobar"></include>
</RelativeLayout>
<RelativeLayout
style="@style/TitleBar"
android:id="@+id/title_bar"
android:layout_below="@+id/logo_bar">
<include layout="@layout/titlebar"></include>"
<Button style="@style/TitleBarButton"
android:id="@+id/vm_speaker_btn"
android:text="@string/vm_speaker_btn_label"
android:layout_alignParentLeft="true"
android:layout_margin="4dp">
</Button>
<Button style="@style/TitleBarButton"
android:id="@+id/vm_edit_btn"
android:text="@string/vm_edit_btn_label"
android:layout_alignParentRight="true"
android:layout_margin="4dp">
</Button>
</RelativeLayout>
<ListView
android:id="@+id/@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title_bar"/>
<RelativeLayout
android:id="@+id/vm_control_panel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/footer"
android:visibility="gone">
<include layout="@layout/vm_control"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/footer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<include layout="@layout/taskbar"/>
</RelativeLayout>
</RelativeLayout>
Here is the layout for the included control
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id="@+id/vm_control"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/dark_grey">
<TextView
android:id="@+id/vm_ctl_branding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="@string/branding"
android:textColor="@color/white">
</TextView>
<TextView style="@style/TimeMark"
android:id="@+id/vm_timestamp"
android:layout_toLeftOf="@+id/vm_progress"
android:layout_below="@+id/vm_ctl_branding"
android:layout_marginRight="3dp"
android:text="0:00">
</TextView>
<SeekBar
android:id="@+id/vm_progress"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/vm_ctl_branding"
android:layout_centerHorizontal="true">
</SeekBar>
<TextView style="@style/TimeMark"
android:id="@+id/vm_timeleft"
android:layout_toRightOf="@+id/vm_progress"
android:layout_below="@+id/vm_ctl_branding"
android:layout_marginLeft="3dp"
android:text="-0:00">
</TextView>
<LinearLayout
android:id="@+id/vm_action_button_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/vm_timestamp"
android:layout_alignRight="@+id/vm_timeleft"
android:orientation="horizontal"
android:layout_below="@+id/vm_progress">
<TextView style="@style/Vm_Action_Button"
android:background="@drawable/vm_action_btn_call"
android:id="@+id/vm_callback_btn"
android:layout_marginRight="5dp"
android:text="@string/vm_action_btn_callback">
</TextView>
<TextView style="@style/Vm_Action_Button"
android:background="@drawable/vm_action_btn_delete"
android:id="@+id/vm_delete_btn"
android:layout_marginLeft="5dp"
android:text="@string/vm_action_btn_delete">
</TextView>
</LinearLayout>
</RelativeLayout>
</merge>
From my onCreate method...
// Handle list item selections
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Voicemail vmItem = (Voicemail) vmla.getItem(position);
Toast.makeText(ctx, "Name: " + vmItem.getCallerName() + " Position: " + position, Toast.LENGTH_SHORT)
.show();
vVm_controls.startAnimation(mSlideIn);
}
});
And my animation callbacks...
@Override
public void onAnimationStart(Animation animation) {
vm_controls_in = !vm_controls_in;
if (vm_controls_in) {
vVm_controls.setVisibility(View.GONE);
} else {
vVm_controls.setVisibility(View.VISIBLE);
// vVm_controls.bringToFront();
}
}
@Override
public void onAnimationEnd(Animation animation) {
if (!vm_controls_in) {
try {
Thread.sleep(1000);
vVm_controls.startAnimation(mSlideOut);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
As you discovered, the issue is visibility. When a view is initially set as gone in the xml file, Android will not render the layout until the visibility is changed to visible or invisible. If you attempt to animate on a view that has not been rendered yet, the animation will occur on the view without a layout. After the animation has completed, it will render it and suddenly the view will appear without animation. It works subsequent times because the view has a layout even when set to gone.
The key is to set the visibility to invisible instead of gone in the xml file so that it is rendered yet hidden. When the animation occurs the first time the view appear and will move as expected.
这篇关于布局动画不工作的第一次运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!