默认情况下,我的CardView
底部带有支持文字GONE
。我想仅在用户点击"操作箭头"时才能看到卡片的这一部分,如下图所示:
我知道只需将View
可见性设置为VISIBLE
即可实现这一目标,但我也希望为展开和展开事件设置动画。
为此,我在我的android:animateLayoutChanges="true"
xml上使用了CardView
属性,它在扩展时效果很好。但是,一旦我再次点击箭头折叠支持文本,下面的卡片就会重叠我在动画期间点击过的卡片。我该如何避免这种重叠?
编辑:我知道可能会执行solution on this question之类的操作,但由于android:animateLayoutChanges
选项存在,它似乎过于复杂。我想知道是否可以使用该XML属性解决我的问题,以保持简单。
我的动画代码如下:
Java代码
protected void expandCard() {
if (isExpanded) {
ibt_show_more.animate().rotation(0).start();
isExpanded = false;
tv_support.setVisibility(View.GONE);
}
else {
ibt_show_more.animate().rotation(180).start();
isExpanded = true;
tv_support.setVisibility(View.VISIBLE);
}
}
XML代码
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_small"
card_view:cardCornerRadius="2dp"
android:id="@+id/os_list_item_cv">
<RelativeLayout
android:id="@+id/os_list_item_rl_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<!-- Here goes the header, the image, the action buttons and so on -->
<!-- Omitted on purpose -->
<!-- ... -->
<!-- This is the support TextView -->
<TextView
android:id="@+id/tv_support"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/os_list_item_rl_actions"
android:text="@string/bacon_ipsum"
android:paddingBottom="24dp"
android:paddingEnd="16dp"
android:paddingRight="16dp"
android:paddingLeft="16dp"
android:paddingStart="16dp"
android:visibility="gone"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
答案 0 :(得分:15)
在更改可见性之前,添加以下代码行:
TransitionManager.beginDelayedTransition(the rootView containing the cardView, new AutoTransition());
你应该得到一个流畅的动画。同时删除&#34; animateLayoutChanges = true&#34;在此之前从你的xml。
至于为什么会这样,调用TransitionManager.beginDelayedTransition()会使TransitionManger捕获父ViewGroup中的当前值,并在下一个动画帧中渲染动画。在这种情况下传递的Transition是AutoTransition,它负责处理父ViewGroup中的所有淡入淡出,移动和调整大小。
请参阅Transitions和TransitionManager
还要注意在适当的地方使用支持库中的Transitions,或者执行必要的API级别检查。