android - 单击时使用 TextView 扩展 cardView

标签 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

最佳答案

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

首先像下面这样创建 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>

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

您可以找到完整的实现 Here

关于android - 单击时使用 TextView 扩展 cardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36952436/

相关文章:

Java - SQLite - 如何在 where 子句中使用一列的一组值?

android - 每次更改 Google 标签管理器标签时,我们是否需要在 Android 端更新容器 json 文件

android - 在 cmd 上构建 android 平台时出错

xml - 在 XSLT 中创建 map

java - Android——从 findViewById(R...) 分配的 SurfaceView 不允许访问自定义字段

python - 如何收集同一元素级别的文本节点?

android - 将 TextView 放在 ImageView 的中央?

android - 无法以编程方式选择 tableRow 的子项(textview)

java - 尝试从 ImageView 的 onTouchListener 获取位图像素

android - 特定字体的文本从前面被截断