【发布时间】:2018-02-15 21:37:45
【问题描述】:
我想构建使用回收器视图和卡片视图的应用程序,如下图所示,当使用单击项目时,它将展开,当用户单击其他项目时,该项目将消耗该已消耗项目将折叠。 例如:我点击第 2 项,第 2 项将展开,然后单击第 4 项,第 2 项将折叠,第 4 项将展开。
非常感谢您的回答!
【问题讨论】:
标签: android android-recyclerview collapse cardview
我想构建使用回收器视图和卡片视图的应用程序,如下图所示,当使用单击项目时,它将展开,当用户单击其他项目时,该项目将消耗该已消耗项目将折叠。 例如:我点击第 2 项,第 2 项将展开,然后单击第 4 项,第 2 项将折叠,第 4 项将展开。
非常感谢您的回答!
【问题讨论】:
标签: android android-recyclerview collapse cardview
你可以使用 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);
}
【讨论】: