单击时使用TextView扩展cardView

时间:2016-04-30 08:04:28

标签: android xml textview android-animation android-cardview

我想用点动画展开我的CardView,并显示其余的TextView。

这是我的.xml文件

<LinearLayout android:id="@+id/yolo"
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:card_view="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">


<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
    android:id="@+id/card_view1"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:layout_marginBottom="10dp"
    android:layout_marginEnd="20dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="10dp"
    android:longClickable="true"
    android:orientation="vertical"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp"
    card_view:cardUseCompatPadding="true"
    card_view:contentPadding="10dp"
    >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:card_view="http://schemas.android.com/apk/res-auto"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical">

        <TextView
            android:id="@+id/info_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FF4444"
            android:maxLines="1"
            android:text="Did you know? Arnold Schwarzennegger was born in the Austria, Europe. "
            android:textAlignment="center"
            android:textSize="24sp"
            />

    </LinearLayout>
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
    android:id="@+id/card_view2"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:layout_marginBottom="10dp"
    android:layout_marginEnd="20dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="10dp"
    android:longClickable="true"
    android:orientation="vertical"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp"
    card_view:cardUseCompatPadding="true"
    card_view:contentPadding="10dp"
    >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:card_view="http://schemas.android.com/apk/res-auto"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical">

        <TextView
            android:id="@+id/info_text2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:text="Did you know? Arnold Schwarzennegger was born in the Austria, Europe. "
            android:textAlignment="center"
            android:textColor="#FF4444"
            android:textSize="24sp"
            />

    </LinearLayout>
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
    android:id="@+id/card_view3"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:layout_marginBottom="10dp"
    android:layout_marginEnd="20dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="10dp"
    android:longClickable="true"
    android:orientation="vertical"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp"
    card_view:cardUseCompatPadding="true"
    card_view:contentPadding="10dp"
    >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:card_view="http://schemas.android.com/apk/res-auto"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical">

        <TextView
            android:id="@+id/info_text3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:text="Did you know? Arnold Schwarzennegger was born in the Austria, Europe. "
            android:textAlignment="center"
            android:textColor="#FF4444"
            android:textSize="24sp"
            />

    </LinearLayout>
</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:layout_marginBottom="10dp"
    android:layout_marginEnd="20dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="10dp"
    android:longClickable="true"
    android:orientation="vertical"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="4dp"
    card_view:cardUseCompatPadding="true"
    card_view:contentPadding="10dp"
    >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:card_view="http://schemas.android.com/apk/res-auto"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:orientation="vertical">

        <TextView
            android:id="@+id/info_et"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:text="Did you know? Arnold Schwarzennegger was born in the Austria, Europe. "
            android:textAlignment="center"
            android:textColor="#FF4444"
            android:textSize="24sp"
            />

    </LinearLayout>
</android.support.v7.widget.CardView>

这就是它的外观。我想让它扩展,以便您可以在TextView中看到其他信息,并带有一些扩展动画。

enter image description here

2 个答案:

答案 0 :(得分:2)

我用以下结构做同样的事情:

首先创建如下的AnimationHelper类:

public class AnimationHelper {
    public static void expand(final View v) {
        v.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        final int targetHeight = v.getMeasuredHeight();

        v.getLayoutParams().height = 0;
        v.setVisibility(View.VISIBLE);
        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                v.getLayoutParams().height = interpolatedTime == 1
                        ? LinearLayout.LayoutParams.WRAP_CONTENT
                        : (int) (targetHeight * interpolatedTime);
                v.requestLayout();
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // 1dp/ms
        a.setDuration((int) (targetHeight / v.getContext().getResources().getDisplayMetrics().density));
        v.startAnimation(a);
    }

    public static void collapse(final View v) {
        final int initialHeight = v.getMeasuredHeight();

        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if (interpolatedTime == 1) {
                    v.setVisibility(View.GONE);
                } else {
                    v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
                    v.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        // 1dp/ms
        a.setDuration((int) (initialHeight / v.getContext().getResources().getDisplayMetrics().density));
        v.startAnimation(a);
    }
}

然后在你的xml中放置你要扩展的布局,如下所示(在我的情况下,它是view_popup_menu_expand):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/default_background"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/card_background">


            <TextView
                android:id="@+id/btn_expand"
                style="@style/IconFont.Large"
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:layout_gravity="right"
                android:text="@string/icon_chevron_up"
                android:textColor="@color/color_primary_dark" />




    </android.support.v7.widget.CardView>

    <include layout="@layout/view_popup_menu_expand" />

</LinearLayout>

然后设置一个监听器并将你的view_popup_menu_exapand传递给AnimationHelper.expand()

您可以找到完整的实施Here

答案 1 :(得分:1)

你可以这样做,使用 ExpandableHeightListView 进行视图展开和折叠点击

检查https://github.com/PaoloRotolo/ExpandableHeightListView

检查此代码

<com.github.paolorotolo.expandableheightlistview.ExpandableHeightListView
        android:id="@+id/expandable_listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">
    </com.github.paolorotolo.expandableheightlistview.ExpandableHeightListView>