簡體   English   中英

當我為這個按鈕設置可見性 GONE 時有一個混蛋

[英]There is a jerk when I set visibility GONE for this button

當我設置可見性消失時,看到這張圖片有一個混蛋

我正在使用 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);
}

到目前為止,我已經嘗試了很多解決方案,包括視圖動畫、對象動畫、可擴展布局(通過庫),但到目前為止還沒有運氣! 期待您的建議。 我有一個解決方法來提升卡,但我想讓我的卡從上面展開,就像上圖一樣。

在子節點上使用自動布局動畫時,結果將是,一旦子節點是View.GONE ,父節點就會捕捉到layout_alignParentBottomwrap_content

結合兩個動畫可以讓它不那么生澀。 我的意思是, CardView需要它的高度動畫; 同步或一旦子節點是View.GONE 實際上,當將子節點的高度設置為 0dp 時,這也會為父節點的高度設置動畫,因為它是wrap_content 為高度設置一個靜態值也可以防止它像那樣捕捉。 同樣動畫到View.INVISBLE然后動畫它的高度會阻止父節點捕捉。 很抱歉沒有提供現成的解決方案,但解釋為什么它的行為如此相似以及它如何工作仍然比沒有答案要好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM