【问题标题】:Expanding cardView with a TextView on click单击时使用 TextView 扩展 cardView
【发布时间】:2016-04-30 08:04:28
【问题描述】:

我想在点击时用一点动画扩展我的 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 中看到其他信息,并带有一点展开动画。

【问题讨论】:

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


    【解决方案1】:

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

    首先创建 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

    【讨论】:

      【解决方案2】:

      您可以通过使用 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>
      

      【讨论】:

      • 你得到你想要的了吗?@hyrulelink16
      • 不完全。我想让一个文本视图在点击时用一点动画展开和折叠,而不是让它们可见或不可见。只需为其余的文本视图复制该代码即可。
      • 使用 ExpandableHeightListView
      • 嗯,我不知道你是否理解我的意思。我有一个带有 6 行文本的 textView。我想让一行只可见,当你点击 textView 时,它会通过展开动画显示所有行。所以它看起来像一个问题,答案类型的卡片。
      • 为此使用我的回答中所写的 ExpandableHeightListView
      猜你喜欢
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      相关资源
      最近更新 更多