【发布时间】:2018-08-09 19:59:40
【问题描述】:
我想要做什么
我有一个RecyclerView,其中有很多项目基本上都是CardView。
这些卡片在它们的正文中间有一个支持文本,默认情况下它的可见性设置为GONE,当我点击卡片右侧的箭头时,它变成了VISIBLE。
我正在尝试在文本显示和折叠时为卡片设置动画。
下图显示了展开的卡片和折叠的卡片:
CardView 布局(为了便于阅读,我删除了一些部分):
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="3dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true"
android:id="@+id/root">
<LinearLayout
android:id="@+id/item_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_vertical_margin">
<!-- The header with the title and the item -->
<TextView
android:id="@+id/body_content"
style="@style/TextAppearance.AppCompat.Medium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:layout_marginBottom="8dp"
android:text="@string/about_page_description"
android:textColor="@color/secondaryText"
android:visibility="gone"/>
<!-- The divider, and the footer with the timestamp -->
</LinearLayout>
</android.support.v7.widget.CardView>
问题
当卡片展开并显示身体 TextView 时,动画正在工作,但是,当我尝试将其折叠回来时,动画下方的卡片与第一个卡片重叠。
示例:
到目前为止我已经尝试过什么
我之前已经问过similar question about this behavior here,但该解决方案不适用于卡片中间的TextView。
负责动画部分的代码位于RecyclerView 适配器内。箭头有一个点击监听器,它调用下面的方法:
private fun toggleVisibility() {
if (bodyContent.visibility == View.GONE || bodyContent.visibility == View.INVISIBLE) {
btSeeMore.animate().rotation(180f).start()
TransitionManager.beginDelayedTransition(root, AutoTransition())
bodyContent.visibility = View.VISIBLE
}
else {
btSeeMore.animate().rotation(0f).start()
TransitionManager.beginDelayedTransition(root, AutoTransition())
bodyContent.visibility = View.GONE
}
}
root 是我的CardView。
我也尝试使用 LinearLayout 而不是卡本身来延迟转换,但这也不起作用。
我怎样才能为我的布局实现这种行为?
【问题讨论】:
-
您是否尝试过手动为文本视图的高度设置动画?
-
我没有。由于delayedTransition是为之前的布局工作的,我觉得没必要,直到现在……
标签: android android-recyclerview kotlin android-animation android-transitions