展开CadrView以显示TextView

时间:2017-12-10 12:03:02

标签: android cardview

我正在尝试实现和扩展CardView,但我有问题 继承了我对cardview的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/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="8dp"
    android:clickable="true"
    android:elevation="5dp"
    android:foreground="?android:attr/selectableItemBackground"
    android:orientation="vertical"
    android:stateListAnimator="@drawable/cardview_lift_on_touch">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/product_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@drawable/default_network_image"/>

        <TextView
            android:id="@+id/product_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="16dp"
            android:paddingBottom="16dp"
            android:paddingLeft="16dp"
            android:paddingTop="24dp"
            android:text="Product Name"
            android:textColor="#000000"
            android:textSize="24dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/product_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="16dp"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:text="A buyer persona is an imaginary customer. It is the person for whom you've developed your product and to whom you'd love to sell it (of course!). He or she represents your target audience, but is much more real than a vague description of some demographics."
            android:textColor="#000000"
            android:textSize="14dp"
            android:visibility="visible" />

        <TextView
            android:id="@+id/product_url"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="24dp"
            android:paddingLeft="16dp"
            android:text="example.syriatel.sy/akrab_eleik"
            android:textColor="#0048ff"
            android:textSize="14dp" />

    </LinearLayout>

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

通过将CardView传递给以下方法,我试图在单击CardView时扩展“产品描述”TextView:

public static void expand(final View v) {

    final int initialHeight = v.getHeight();
    v.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    final int targetHeight = v.getMeasuredHeight();

    v.getLayoutParams().height = 1;
    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) ((initialHeight / v.getContext().getResources().getDisplayMetrics().density))/10);
    v.startAnimation(a);

}

生成的动画最初是平滑的,但突然将视图扩展到最后的高度

我从这个答案中得到了我正在使用的代码:

https://stackoverflow.com/a/13381228/5716823

阅读评论没有帮助。

0 个答案:

没有答案