- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的设置:RecyclerView
显示一个项目列表,每个项目包含几个 TextView
。其中一个 TextView 可以有多行,最初我将 android:maxLines
设置为 1。在项目的点击监听器中,我在最大 1 行和最大无限行之间切换(以显示完整内容)。它工作正常,除了它不是动画。我尝试将 android:animateLayoutChanges
添加到我的 View 层次结构中的不同级别,但仍然没有动画。
请让我知道您对此的看法。
注意:我可以编写手动动画,但我希望 Android 使用此 xml 属性为我处理它。
我的一些代码:
我的列表项布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:padding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
<TextView
android:id="@+id/destination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginEnd="36dp"
android:layout_marginRight="36dp"
android:textStyle="bold"
android:ellipsize="end"
android:maxLines="1"
android:textSize="25sp" />
<ImageButton
android:id="@+id/editButton"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_edit"
android:contentDescription="@string/edit_button_content_descr"
android:background="?attr/selectableItemBackgroundBorderless" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/dateRange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp" />
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/daysLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:background="@drawable/days_left_background"
android:gravity="end"
android:layout_gravity="end"
android:textColor="@android:color/white"
android:textSize="15sp"
android:textStyle="bold"/>
</LinearLayout>
<TextView
android:id="@+id/comments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="15sp"
android:ellipsize="end"
android:maxLines="1" />
</LinearLayout>
</android.support.v7.widget.CardView>
在我的 View 持有者中,我正在扩展 comments
TextView:
rootCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
comments.setMaxLines((comments.getMaxLines() == 1) ? Integer.MAX_VALUE : 1);
}
});
这是我包含 RecyclerView 的 fragment 的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/common_vertical_margin"
android:paddingLeft="@dimen/common_horizontal_margin"
android:paddingRight="@dimen/common_horizontal_margin"
android:paddingTop="@dimen/common_vertical_margin"
xmlns:tools="http://schemas.android.com/tools">
<android.support.v7.widget.RecyclerView
android:id="@+id/tripList"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/list_item_trip" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_add" />
</RelativeLayout>
最佳答案
您没有调用适配器的 notifyItemChanged()
,因此 Android 不知道某些内容已更改并且应该设置动画。因此,您只需在更改 maxLines
属性后添加此方法调用:
rootCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
comments.setMaxLines((comments.getMaxLines() == 1) ? Integer.MAX_VALUE : 1);
notifyItemChanged(viewHolder.getAdapterPosition());
}
});
如果它不起作用,请尝试将 animateLayoutChanges
设置为布局的 Root View - 在您的情况下为 CardView
。
关于android:animateLayoutChanges 在更改 TextView 的 maxLines 时在 RecyclerView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38610368/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!