以编程方式设置CardView高程 [英] Setting CardView elevation programmatically

查看:108
本文介绍了以编程方式设置CardView高程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这种方法是否是最好的,但是我有以下问题:

I do not know if this approach is the best, but I have the following problem:

我希望在单击按钮时使RecyclerView中的列表项(CardView)动画化,以便此按钮,该项目中的其他视图以及CardView的背景发生变化.

I want a list item (a CardView) in a RecyclerView to be animated when it's button was clicked, so that this button, some other views inside this item and the background of the CardView changes.

我从一个自定义的RecyclerView.ItemAnimator捕获了animateChange: (直到现在,动画只是通过animateLayoutChanges在xml中定义的淡入淡出)

I am catching the animateChange from a custom RecyclerView.ItemAnimator: (until now the animation is just a fade defined in the xml via animateLayoutChanges)

public boolean animateChange(@NonNull RecyclerView.ViewHolder oldHolder, @NonNull RecyclerView.ViewHolder newHolder, @NonNull ItemHolderInfo preLayoutInfo, @NonNull ItemHolderInfo postLayoutInfo) {
    Log.d("Animation", "Test");
    final MyAdapter.MyViewHolder viewHolder = (MyAdapter.MyViewHolder) newHolder;

    viewHolder.mButtons.setVisibility(View.GONE);
    viewHolder.mHints.setVisibility(View.GONE);
    viewHolder.mImageView.setVisibility(View.GONE);

    ViewGroup.LayoutParams layoutParams = viewHolder.containerCardView.getLayoutParams();
    layoutParams.height = 192;
    viewHolder.containerCardView.setLayoutParams(layoutParams);
    viewHolder.containerCardView.setBackgroundResource(R.drawable.some_background);
    viewHolder.containerCardView.setElevation(20f);

    return true;
}

如您所见,我正在animateChange中手动设置背景.除了CardView的高程之外,其他所有东西都可以正常运行(或者至少可以说是按预期进行).我尝试手动设置海拔高度,但是没有显示海拔高度.

As you can see, I am setting the background manually in the animateChange. Everything works fine (or lets say at least as expected) except for the elevation of the CardView. I tried to set the elevation by hand, but there is no elevation shown.

EDIT ______

EDIT______

我的CardView里面有一个ImageView和一个按钮.按下按钮时,按钮本身和图片应淡出,并且cardView的右侧应有一条红线.我试图通过将背景(使用setBackgroundResource)设置为此:

My CardView has an ImageView and a button inside it. When pressing the button, the button itself and the picture should fade out and there shall be a red line on the right side of the cardView. I tried to achieve that by setting the background (with setBackgroundResource) to this:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle">
        <gradient
            android:angle="180"
            android:centerColor="@android:color/transparent"
            android:centerX="0.1"
            android:startColor="@android:color/holo_red_dark"/>
        <corners
            android:bottomLeftRadius="2dip"
            android:bottomRightRadius="2dip"
            android:topLeftRadius="2dip"
            android:topRightRadius="2dip"/>
    </shape>
</item>

<item android:right="4dp">
    <shape android:shape="rectangle">
        <solid android:color="@android:color/white" />
    </shape>
</item>
</layer-list>

这只是一个可绘制的xml,在右边带有红线.您对实现我的目标的另一种方法有何建议?

Which is just an xml drawable with a red line on the right. Do you have any suggestions for another way to approach my goal?

推荐答案

您应致电 setCardElevation 而不是setElevation.第二个修改属性 elevation ,该属性所有View子类都具有Lollipop(API级别21).

You should call setCardElevation rather than setElevation. The second one modifies the property elevation, which all View subclasses have since Lollipop (API level 21).

并且请勿使用 setBackgroundResource:卡的背景是其高程(这是因为卡与Lollipop之前的所有Android版本兼容,但不具有elevation属性) .使用setCardBackgroundColor设置背景颜色(或xml中的app:cardBackgroundColor).

And DON'T USE setBackgroundResource: the card background is its elevation (this because the card is compatible with all Android versions before Lollipop, which don't have the elevation property). Use setCardBackgroundColor to set a background color (or app:cardBackgroundColor in your xml).

如果您需要背景图片而不是颜色,请在卡中放置ImageView作为此答案建议.

If you need a background image, rather than a color, place an ImageView inside of your card as this answer suggests.

做您想做的事,但是不要在CardView上调用任何setBackground...方法,否则您将失去卡阴影(其提升效果).

Do what you want, but DON'T call any setBackground... method on the CardView, or you will lose the card shadow (its elevation effect).

第二,使用以下代码设置高程的动画:

Secondly, to animate your elevation use the following code:

ObjectAnimator animator = ObjectAnimator.ofFloat(cardView, "cardElevation", start, end);
// if needed set duration, interpolator, or what you want on the animator
animator.start();

这篇关于以编程方式设置CardView高程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆