【问题标题】:how to expend and collapse card view in RecyclerView in android when click other item单击其他项目时如何在android的RecyclerView中展开和折叠cardview
【发布时间】:2018-02-15 21:37:45
【问题描述】:

我想构建使用回收器视图和卡片视图的应用程序,如下图所示,当使用单击项目时,它将展开,当用户单击其他项目时,该项目将消耗该已消耗项目将折叠。 例如:我点击第 2 项,第 2 项将展开,然后单击第 4 项,第 2 项将折叠,第 4 项将展开。

非常感谢您的回答!

【问题讨论】:

    标签: android android-recyclerview collapse cardview


    【解决方案1】:

    你可以使用 Expandable recycler view (在 github 上可用)。如果你想使用 Recycler 视图,你可以像切换一样使用动画展开和折叠视图。查看下面的代码:

     public static void expandCard(final View v) {
        v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.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
                        ? ViewGroup.LayoutParams.WRAP_CONTENT
                        : (int)(targetHeight * interpolatedTime);
                v.requestLayout();
            }
            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        a.setDuration(((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density))*6);
        v.startAnimation(a);
    }
    
    public static void collapseCard(final View v) {
        final int initialHeight = v.getMeasuredHeight();
        Animation a = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if(interpolatedTime == 1){
                    v.setVisibility(View.GONE);
                }else{
                    v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                    v.requestLayout();
                }
            }
            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        a.setDuration(((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density))*6);
        v.startAnimation(a);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多