【问题标题】:There is a jerk when I set visibility GONE for this button当我为此按钮设置可见性 GONE 时有一个混蛋
【发布时间】:2020-03-30 07:59:40
【问题描述】:

我正在使用 android:animateLayoutChanges="true" 我在这里分享我的卡片视图布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="vertical">

    <androidx.cardview.widget.CardView
        android:id="@+id/cvRegisteredPharmacy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="30dp"
        app:cardBackgroundColor="@color/colorAccent"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/ivPharmacy"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_weight="0.25"
                android:background="@color/colorWhite" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="15dp">

                <TextView
                    android:id="@+id/tvPharmacyName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:text="Pharmacy Name"
                    android:textColor="@color/colorWhite"
                    android:textSize="16sp"
                    android:textStyle="bold" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <RatingBar
                        android:id="@+id/rbPharmacyRating"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:numStars="5"
                        style="?android:attr/ratingBarStyleSmall"
                        android:rating="3.5"
                        android:progressTint="@color/colorPrimary"
                        android:progressBackgroundTint="@color/colorGrey" />


                <TextView
                    android:id="@+id/tvPharmacyInfo"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_marginLeft="5dp"
                    android:text="3.5"
                    android:textColor="@color/colorWhite"
                    android:textSize="12sp" />

                </LinearLayout>

                <TextView
                    android:id="@+id/tvPharmacyAddress"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="Pharmacy Address, 1/2 Main St NW, Monteral, QC"
                    android:textColor="@color/colorWhite"
                    android:textSize="14sp" />

            </LinearLayout>

            <Button
                android:id="@+id/btnSelectPharmacy"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginBottom="15dp"
                android:background="@drawable/btn_bg_white"
                android:text="Select Pharmacy"
                android:textColor="@color/colorAccent"
                android:visibility="gone" />

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

这是我制作的完整布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.PharmacyMapActivity">

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rvPharmaciesList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"/>

</RelativeLayout>

另外,我正在分享设置此按钮可见性的简单代码:

if (holder.btnSelectPharmacy.getVisibility() == View.VISIBLE){
                    holder.btnSelectPharmacy.setVisibility(View.GONE);
                } else {
                    holder.btnSelectPharmacy.setVisibility(View.VISIBLE);
}

到目前为止,我已经尝试了许多解决方案,包括视图动画、对象动画器、可扩展布局(通过库),但到目前为止还没有成功! 期待您的建议。 我有一个解决方法来提升卡,但是我想让我的卡从上方展开,就像上图一样。

【问题讨论】:

  • 能不能也分享一下全屏的布局代码?
  • 如果我让它不可见,它将占用额外的空间@MartinZeitler
  • 我不太确定您是否真的需要 CardView 周围的 LinearLayout。一个要点可能是将 layout_gravity="bottom" 添加到父布局中。这就是为什么我要求提供整个屏幕的布局代码的原因。
  • 让我编辑问题@JulianSchweppe 是的,我正在使用布局重力底部,因为我需要在屏幕底部使用这张卡片
  • @JulianSchweppe 我已按照您要求的布局编辑了我的帖子

标签: java android android-animation


【解决方案1】:

在子节点上使用自动布局动画时,结果将是,一旦子节点为View.GONE,父节点就会捕捉到layout_alignParentBottomwrap_content

结合两个动画可以减少生涩。我的意思是,CardView 需要它的高度动画;同步或一旦子节点为View.GONE。实际上,当将子节点的高度设置为 0dp 时,这也会为父节点的高度设置动画,因为它是 wrap_content;为高度设置一个静态值也可以防止它像那样捕捉。同样动画到View.INVISBLE 然后动画它的高度会阻止父节点捕捉。很抱歉没有提供现成的解决方案,但解释它为什么会这样以及它是如何工作的仍然比没有答案要好。

【讨论】:

  • 感谢您的回答 Martin Zeitler,我知道原因但无法解决。你能以编程方式回答吗?
猜你喜欢
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 2011-09-02
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-17
相关资源
最近更新 更多