更改TextView

时间:2016-07-27 10:27:27

标签: android animation textview android-recyclerview

我有一个简单的设置:RecyclerView显示一个项目列表,每个项目包含多个TextView个。其中一个文本视图可以有多行,最初我将android:maxLines设置为1.在项目的点击监听器中,我在最多1行和最大无限之间切换(显示完整内容)。它工作正常,除了它没有动画。我尝试将android:animateLayoutChanges添加到视图层次结构中的不同级别,但仍然没有动画。

请让我知道你对此的看法。

注意:我可以编写手动动画,但我希望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>

在我看来,我正在展开comments TextView:

    rootCard.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            comments.setMaxLines((comments.getMaxLines() == 1) ? Integer.MAX_VALUE : 1);
        }
    });

以下是包含RecyclerView的片段的布局:

<?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>

1 个答案:

答案 0 :(得分:3)

您没有调用适配器的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设置为布局的根视图 - 在您的情况下,它是CardView