执行操作时如何移动布局或卡片视图

How can I move a layout or card view when an action is made

我在 android 工作室中通过更改可展开卡片视图的可见性创建了可展开和可折叠卡片视图。 (因此,如果它可见,它就会展开,如果不可见,它就会折叠,每个卡片视图都有一个状态,即它是展开还是折叠。) 但是当卡片视图展开时,它就会位于下一个卡片视图的下方。有什么可能的方法可以将折叠的卡片视图从展开的卡片视图中推开?

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout    
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp" --> the extra 100dp for the expanded view
android:orientation="vertical"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_marginBottom="-100dp"> --> this allows the card views to be underneath each other when not expanded
....

</LinearLayout>

我在卡片视图适配器中的代码...

private void onClickButton(final CardView expandableLayout, final       
LinearLayout mainlayout, final int i){

    if (expandableLayout.getVisibility() == View.VISIBLE){
        expandableLayout.setVisibility(View.GONE);
        expandState.put(i,false);
    }
    else{
        expandableLayout.setVisibility(View.VISIBLE);

        // ideally mainlayout. 
        // something to change the   android:layout_marginBottom="0" ?
        expandState.put(i,true);
    }
}

我在想我需要实现一些代码来更改主要布局参数,当它可见和不可见时,我只是找不到要使用的代码。

这是因为您将线性布局设置为

android:layout_height="200dp"

线性布局无法展开超过 200dp

尝试 'wrap_content' LinearLayout 高度

更好的是计算每个高度。

当您展开视图时,它会扩展到其他视图的任何高度 + CardView 的额外高度

使用 ValueAnimator 为高度设置动画,如下所示:

ValueAnimator anim = ValueAnimator.ofInt(YOUR_LINEAR_LAYOUT.getHeight(), getResources().getDimensionPixelSize(R.dimen.YOUR_DESIRED_HEIGHT));
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  @Override
  public void onAnimationUpdate(ValueAnimator valueAnimator) {
    int Val = (Integer) valueAnimator.getAnimatedValue();
    ViewGroup.LayoutParams param = YOUR_LINEAR_LAYOUT.getLayoutParams();
    param.height = Val;
    YOUR_LINEAR_LAYOUT.setLayoutParams(param);
  }
});

anim.setInterpolator(YOUR_DESIRED_INTERPOLATOR);
anim.setDuration(DURATION).start();

您甚至可以在 AnimatorSet

中使用多个值动画器