【问题标题】:android listview change height after animationandroid listview在动画后改变高度
【发布时间】:2018-09-02 18:49:05
【问题描述】:

我的 LinearLayout 中有一个 Button、一个 View 和一个 ListView。在按钮单击事件上,我希望视图消失并且列表视图取而代之。我试图在动画之后将 LayoutParams 设置为 match_parent,但它不起作用。有什么想法吗?

这是我的activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/container"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.example.linearlayouttry.MainActivity" >

 <Button android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start"/>

 <View android:id="@+id/view"
    android:layout_width="fill_parent"
    android:background="@android:color/black"
    android:layout_height="300dp"/>

 <ListView android:id="@+id/listv"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 </ListView>

</LinearLayout>

这是我的 MainActivity.java:

public class MainActivity extends Activity implements OnClickListener {

protected View v;
protected ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<String> list = new ArrayList<String>(Arrays.asList("1","2","3","4","5","6","7","8","9","10"));
    lv = (ListView) findViewById(R.id.listv);
    lv.setAdapter(new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list));

    Button mButton = (Button) findViewById(R.id.button);
    mButton.setOnClickListener(this);

    v = findViewById(R.id.view);
}

@Override
public void onClick(View v) {
    this.v.setAlpha(0);
    int height = this.v.getLayoutParams().height;
    lv.animate().translationY(-height).setListener(new AnimatorListener() {
        public void onAnimationStart(Animator animation) {}
        public void onAnimationRepeat(Animator animation) {}

        @Override
        public void onAnimationEnd(Animator animation) {
            lv.getLayoutParams().height=LayoutParams.MATCH_PARENT;
            lv.requestLayout();
        }

        public void onAnimationCancel(Animator animation) {}
    });
}
}

当我将特定整数设置为高度时它会起作用,但我如何使用 match_parent 来做到这一点?

已解决:

我只需要像这样从线性布局中删除视图:

public void onAnimationEnd(Animator animation) {
            parent.removeView(this.v);
            lv.setTranslationY(0);
}

【问题讨论】:

  • 即使隐藏另一个视图也足够好了,比如 this.v.setVisibility(View.GONE)

标签: android android-layout listview layoutparams


【解决方案1】:

试试

lv.setLayoutParams(new LayoutParams(widthValue, LayoutParams.MATCH_PARENT));

而不是

lv.getLayoutParams().height=LayoutParams.MATCH_PARENT;
lv.requestLayout();

更多关于 LayoutParams 类的信息可以在here找到。

【讨论】:

  • 谢谢,我试过了,但是如果我写了new LinearLayout.LayoutParams(),它就退出了ClassCastException,它就不能再工作了:(。
【解决方案2】:

感谢您的回答,我解决了从线性布局中删除视图的问题,如下所示:

public void onAnimationEnd(Animator animation) {
            parent.removeView(this.v);
            lv.setTranslationY(0);
}

【讨论】:

    【解决方案3】:

    或者,

    public void onAnimationEnd(Animator animation) {
         this.v.setVisibility(View.GONE);
         lv.setTranslationY(0);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      • 1970-01-01
      相关资源
      最近更新 更多