java - CardView 无法正确展开

标签 java android xml animation cardview

card screenshot

我有一个扩展 CardView 的 TimerView 类。它最初折叠以仅显示第一行,然后点击即可展开。问题是它没有得到应有的扩展。在屏幕截图中,您可以看到底部图像比其他图像小,但三张图像的分辨率都是相同的。

这是使其扩展的代码。 clicked 变量是静态的,我用它来测量卡片的高度一次,因为所有卡片都是相同的。

@Override
protected void onFinishInflate(){
    super.onFinishInflate();

    RelativeLayout upperCard = findViewById(R.id.upper_card);

    upperCard.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
                LinearLayout subCard = findViewById(R.id.sub_card);

                if(!clicked){
                    foldedHeight = getMeasuredHeightAndState();
                    subCard.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    targetHeight = foldedHeight + subCard.getMeasuredHeightAndState();
                    clicked = true;
                }

                if(subCard.getVisibility() == View.GONE) {
                    subCard.setVisibility(View.VISIBLE);
                    ImageView arrow = findViewById(R.id.arrow);
                    arrow.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_keyboard_arrow_up_black_24dp));
                    ValueAnimator valueAnimator = ValueAnimator.ofInt(foldedHeight, targetHeight);
                    valueAnimator.setDuration(200);
                    valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
                    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        public void onAnimationUpdate(ValueAnimator animation) {
                            Integer value = (Integer) animation.getAnimatedValue();
                            getLayoutParams().height = value.intValue();
                            requestLayout();
                        }
                    });
                    valueAnimator.start();
                }
...

这是与TimerView相关的xml文件。

<com.whatever.marco272.timepassedsince.TimerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="5dp"
card_view:contentPadding="7dp"
card_view:cardElevation="3dp"
card_view:cardUseCompatPadding="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/upper_card"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_marginRight="24dp"
            android:textSize="24sp"
            android:textStyle="normal|bold"
            android:textColor="@android:color/holo_blue_dark"
            android:text="Title" />

        <TextView
            android:id="@+id/days"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/title"
            android:layout_gravity="bottom"
            android:textSize="24sp"
            android:textStyle="normal|bold"
            android:textColor="@android:color/black"
            android:text="0" />

        <ImageView
            android:id="@+id/arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:src="@drawable/ic_keyboard_arrow_down_black_24dp"/>

</RelativeLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sub_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="gone">

        <TextView
            android:id="@+id/clock"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:textSize="20sp"
            android:textColor="@android:color/black"
            android:text="0" />

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/add_button"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginBottom="10dp"
            android:gravity="center_vertical">
            <ImageView
                android:src="@drawable/ic_add_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:text="Update"/>
        </LinearLayout>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/edit_button"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginBottom="10dp"
            android:gravity="center_vertical">
            <ImageView
                android:src="@drawable/ic_edit_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:text="Edit"/>
        </LinearLayout>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/delete_button"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:gravity="center_vertical">
            <ImageView
                android:src="@drawable/ic_delete_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:text="Delete"/>
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

最佳答案

我怀疑这与 CardView 的填充有关。在 Lollipop 之前的 API 级别上,CardView 会覆盖您为了插入卡片而设置的任何填充(以便它可以在 View 边界内绘制“阴影”)。由于您已设置 card_view:cardUseCompatPadding="true" 属性,因此在 Lollipop 及以上版本上也会发生这种情况。

我相信此填充可以解释展开时“缺失”的高度。

关于java - CardView 无法正确展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48215930/

相关文章:

android - ARCore:如何在没有平面跟踪的情况下从 session 对象创建 anchor ?

java - 解码错误 : unexpected element (uri: local:), 预期元素为

java - 了解简单 XML 解析器 - 新文件输出 - Java

java - Jackson 使用可选字段的默认值反序列化记录

java - 数组比较字符串

java - 如何修复此代码以在 Android 应用程序的 Activity 之间传递数据?

javascript - 如何使用 XML 文档更新 HTML 文本?

java - DocumentFilter 的每个方法(及其参数)到底做什么?

java - 如何在不创建额外的 WUF 对象或使用复杂度 >= O(n) 的方法的情况下处理渗透中的反冲洗问题?

android - 不幸的是,androiddemo(项目名称)已经停止