【问题标题】:Android: how to expand and collapse cardView size in RecyclerViewAndroid:如何在 RecyclerView 中展开和折叠 cardView 大小
【发布时间】:2015-08-10 04:33:57
【问题描述】:

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

【问题讨论】:

    标签: android


    【解决方案1】:

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

    设置我对onCreateView()的看法

    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 Height 设置为展开和折叠。

    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();
        }
    }
    

    【讨论】:

    • 此卡片视图中的 detailProductDescription 是什么。它在哪里初始化
    • 它是一个变量(如卡片视图),您可以通过在任何位置查找视图 id 来扩展视图并确定它的变量名称。
    猜你喜欢
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    相关资源
    最近更新 更多