Android:如何在 RecyclerView 中展开和折叠 cardView 大小

标签 android

我需要在运行时展开和折叠 RecyclerView 中的 CardView 大小。 我使用 RecyclerView 显示列表,使用 CardView 作为 RecyclerView 项。 由于我是这个概念的新手,请帮助我。

最佳答案

我已经扩展了运行时间。像这样,

onCreateView() 上设置我的 View

descriptionCardView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver
    .OnPreDrawListener() {
@Override
public boolean onPreDraw() {
    detailProductDescription.getViewTreeObserver().removeOnPreDrawListener(this);
    // save the full height
    descriptionViewFullHeight = detailProductDescription.getHeight();

    // initially changing the height to min height
    ViewGroup.LayoutParams layoutParams = descriptionCardView.getLayoutParams();
    layoutParams.height = (int) getActivity().getResources().getDimension(R.dimen
            .product_description_min_height);
    descriptionCardView.setLayoutParams(layoutParams);

    return true;
 }
});

和onClickingcardview

@OnClick(R.id.detail_product_description)
void onProductDescriptionClicked(View view) {
    toggleProductDescriptionHeight();
}

我正在设置 CardView 的高度以展开和折叠。

private int descriptionViewFullHeight;    
private void toggleProductDescriptionHeight() {

    int descriptionViewMinHeight = (int) getActivity().getResources().getDimension(R.dimen
            .product_description_min_height);
    if (descriptionCardView.getHeight() == descriptionViewMinHeight) {
        // expand
        ValueAnimator anim = ValueAnimator.ofInt(descriptionCardView.getMeasuredHeightAndState(),
                descriptionViewFullHeight);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = descriptionCardView.getLayoutParams();
                layoutParams.height = val;
                descriptionCardView.setLayoutParams(layoutParams);
            }
        });
        anim.start();
    } else {
        // collapse
        ValueAnimator anim = ValueAnimator.ofInt(descriptionCardView.getMeasuredHeightAndState(),
                descriptionViewMinHeight);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = descriptionCardView.getLayoutParams();
                layoutParams.height = val;
                descriptionCardView.setLayoutParams(layoutParams);
            }
        });
        anim.start();
    }
}

关于Android:如何在 RecyclerView 中展开和折叠 cardView 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31911684/

相关文章:

java - 线程连接在 Android 中不起作用?

java - Android 上的 OpenGL : Any conflicts when calling OpenGL functions in both Java and C++?

Android 慢 GridView

java - 插入ArrayAdapter和添加到arrayList有什么区别?

android - 在没有永久监控的情况下在 Android 上获取电池电量的最佳方法是什么?

android - 通过应用内结算捐款

java - Android 线程、锁、并发示例

android - java.lang.NoClassDefFoundError : Failed resolution of: Landroid/support/v4/util/ArrayMap; 错误

android - 应用程序在后台运行时查找当前位置不起作用

android - 我无法将AppCompat-v7编译到我的Android Studio项目中