【问题标题】:How can I move a layout or card view when an action is made进行操作时如何移动布局或卡片视图
【发布时间】:2018-03-02 07:29:37
【问题描述】:

我通过更改可展开卡片视图的可见性在 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);
    }
}

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

【问题讨论】:

  • 设置卡片视图展开时的高度
  • 这似乎不起作用它将可扩展的卡片视图设置在它正在扩展的卡片视图之上(我在上面发布的线性布局中的两个卡片视图)但不是其他的我认为这个是因为它们是在循环视图中创建的。

标签: android android-linearlayout android-cardview


【解决方案1】:

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

android:layout_height="200dp"

线性布局不能超过 200dp

为 LinearLayout 高度尝试 'wrap_content'

更好的是计算每个高度。

当您展开视图时,它会展开到其他视图具有的任何高度 + 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 中使用多个 Value Animator

【讨论】:

  • 好吧,我看到它很简单,就像使用包装内容一样,然后我可以按照定义保留卡片视图尺寸。我还必须摆脱 android:layout_marginBottom="-100dp"。我没有尝试过动画师,但会做。
  • 是的,如果您需要更多详细说明和代码示例,请告诉我
猜你喜欢
  • 1970-01-01
  • 2017-08-17
  • 2020-05-24
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 2019-10-31
  • 2023-03-05
  • 1970-01-01
相关资源
最近更新 更多