簡體   English   中英

如何隱藏 CoordinatorLayout 的一些視圖

[英]How to hide some of the views of a CoordinatorLayout

我有一個布局,里面包含很多視圖,假設層次結構是:

  • 協調器布局
    • 嵌套滾動視圖
      • 約束布局
        • 約束布局
          • 一些視圖
        • 視圖(分隔符)
        • 約束布局
          • 一些視圖
        • 視圖(分隔符)
        • 等等...
    • 包括 AppBar
    • 我的自定義進度條
  • 關閉 CoordinatorLayout

問題是我已經創建了一個Group ,其中包含我想要隱藏的 id 的constraint_referenced_ids (它們在層次結構樹中,但是當我嘗試隱藏它們並顯示另一個它們沒有隱藏時,我仍然看到它們。

我的想法是,我希望這個布局就像現在一樣,但是添加一個 ProgressBar,當它加載時它只可見 ProgressBar,例如

我做了一個api調用

進度條可見 我得到了來自 api 的響應 隱藏進度條 使可見 組並填充里面的值

我嘗試將它們添加到CoordinatorLayout ,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myRoot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

   <ConstraintLayout>...</ConstraintLayout>

    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <MyCustomProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:id="@+id/my_custom_progress_bar"/>

    <androidx.constraintlayout.widget.Group
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="lot_of_ids"
        android:id="@+id/myGroup"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

但我無法讓它工作,我錯過了什么?

一般的想法是能夠繼續使用大布局,但只需添加一些視圖,例如ProgressBarEmptyCustomView來顯示它們而不重疊信息,只顯示確切的視圖,我已經做了很多次但不是在CoordinatorLayout ,就像將它與“狀態”分開一樣,當狀態為 Loading 時顯示 progressBar,當狀態為 Empty 時顯示 EmptyCustomView 並且當狀態為 Success 時則顯示布局從開始但隱藏其他人。

我不完全理解你的問題。 但是,我推斷您正在嘗試隱藏一些視圖,並且很容易以編程方式執行此操作。

第 1 步:提供視圖以在 XML 聲明中隱藏 ID。 說它是一個TextView。

<TextView
    android:id="@+id/view_to_hide"
    .../>

第 2 步:在 Java 文件中聲明視圖。

TextView toHide = (TextView) findViewById(R.id.view_to_hide);

第 3 步:無論您需要什么,都可以通過調用setVisibility方法來隱藏視圖。

toHide.setVisibility(View.VISIBLE); //for visible
toHide.setVisibility(View.INVISIBLE); //for invisible

還有其他值,例如View.GONE 但是,我發現VISIBLEINVISIBLE足以滿足我的所有目的。

還要注意,這個方法實際上適用於任何擴展View類的東西(例如,這甚至可以在LinearLayout s 等上調用)。 在設置View.INVISIBLE ,視圖無法獲得焦點且無法單擊。 我希望這個答案有幫助。

暫無
暫無

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

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