【问题标题】:hide the content of fragment when progress bar is visible当进度条可见时隐藏片段的内容
【发布时间】:2020-10-20 08:53:18
【问题描述】:

我的片段包含许多不同的视图。一些视图已经在 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>

【问题讨论】:

  • 你能分享你的xml代码吗?
  • 是的,请查看@MuntasirAonik

标签: android android-layout android-fragments android-progressbar


【解决方案1】:

只是把视图放到其他地方。例如,如果您使用FrameLayout,您可以拥有相当扁平的结构,其中ToolbarProgressBar 与包含其余布局的内部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

【讨论】:

  • XML 文件包含 swipeRefreshLayout,如果我把它放在 frameLayout 中,我无法让它消失
  • 为什么不呢?看到这个:stackoverflow.com/questions/40289196/…
  • 如果我将 swiperefresh 放入 FrameLayout 并使 FrameLayout 消失,所有包含仍然可见我只是尝试过
  • 还有其他问题是当数据来自服务器并且出现一些问题(例如互联网连接)时,在这种情况下我无法刷卡,因为布局已经消失并且刷卡在那个布局中
  • 您检查了评论中的链接吗?如果您调用“setRefreshing(false)”,则可以使 ScrollView 消失。另一件事是,您可以尝试拨打scrollView.visibility = View.INVISIBLE 来检查它是否有效
猜你喜欢
  • 2019-08-17
  • 2021-01-09
  • 2017-09-05
  • 2019-09-20
  • 2017-02-25
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多