0

我的片段包含许多不同的视图。一些视图已经在 XML 文件中,例如imageview,drawables 等,而其他视图在 Retrofit 成功响应后会填充数据。XML 文件还包含一个工具栏和progressbar.

当数据来自服务器时,我只想显示工具栏和进度条并隐藏其他内容。但问题是我无法隐藏整个视图,因为它本身包含工具栏和进度条。

我应该怎么办?

我想要的输出 我得到的输出

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<androidx.appcompat.widget.Toolbar...>

 <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/swiperefresh_open_post"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_sec">

 <!-- this contains all Content I want to hide this -->
    <ScrollView...>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>


 <!-- this contains content which is fixed at the bottom I also want to hide this -->
<FrameLayout...>

<include layout="@layout/progressbar_item" />

</RelativeLayout>
4

1 回答 1

0

只是把视图放到其他地方。例如FrameLayout,如果您使用 ,您可以拥有非常扁平的结构,其中Toolbar和位于与包含其余布局的ProgressBar内部相同的层次结构级别。FrameLayout所以你可以做这样的事情:

<FrameLayout
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="56dp" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="56dp" >

             <!-- Put your layout here -->
    </FrameLayout>

    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone" />
        
</FrameLayout>

然后打电话

container.visibility = View.GONE
progressbar.visibility = View.VISIBLE

或者

container.visibility = View.VISIBLE
progressbar.visibility = View.GONE
于 2020-06-30T05:58:49.897 回答