簡體   English   中英

從 View.GONE 狀態到 View.VISIBLE 的 setVisibility 無法正常工作

[英]setVisibility from View.GONE state to View.VISIBLE not working properly

我有一個真正奇怪的問題,我不知道如何解決它。 我在 LinearLayout 中有一個 View 屬性為 android:visibility="gone" 這是布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/bg"
    android:orientation="vertical">

    <TextView
        android:id="@+id/titleTextView"
        android:gravity="end"
        android:text="dfgdfg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="8dp"
        android:background="@drawable/shadow"
        android:id="@+id/preLollipopShadow"
        android:visibility="gone"/>

</LinearLayout>

當我想使用 preLollipopShadow 更改視圖的可見性時,我使用此代碼

if (newPosition == StickyHeaderLayoutManager.HeaderPosition.STICKY) {
                        ((ViewGroup)header).getChildAt(1).setVisibility(View.VISIBLE);

                    }
                    else
                    {
                        ((ViewGroup)header).getChildAt(1).setVisibility(View.GONE);

                    }

它不像我期望的那樣工作,我希望在粘性模式下可以看到陰影,否則就消失了。

我怎樣才能做到這一點?

更新 1 :當我以“不可見”的可見性開始並切換到“可見”並返回時,它正在工作,但在我的 preLollipopShadow 視圖上沒有“消失”的起始狀態。

更新 2:視圖位於 RecyclerView 內,是否不更新與嵌套到 RecyclerView 中有關的視圖可見性?

您要隱藏和顯示的視圖附加了一個 preLollipopShadow id 很容易通過它的 id 找到視圖。

parentView.findViewById(R.id.preLollipopShadow);

或者

findViewById(R.id.preLollipopShadow);

如果您想從活動中引用視圖。

暫無
暫無

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

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