【问题标题】:ProgressBar is not visible after RecyclerViewRecyclerView 后 ProgressBar 不可见
【发布时间】:2016-05-03 07:49:06
【问题描述】:

我想在使用 RecyclerView 加载更多数据时在列表末尾显示一个进度条。只是为了测试,我将它设置为始终可见,但它根本不可见。我试图将它放在 LinearLayout 中,但这没有帮助。

这是我的片段:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_gravity="center_vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/latestnews_swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView_latestnews"
            android:scrollbars="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>
    <ProgressBar
        android:id="@+id/loadmore_progressBar"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:indeterminate="true"
        android:layout_gravity="center"
        android:visibility="visible" />
</LinearLayout>

Fragment 在 vi​​ewpager 中加载:

        <LinearLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <android.support.design.widget.TabLayout
                android:id="@+id/sliding_tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </LinearLayout>

【问题讨论】:

  • 当然是不可见的,将recyclerView设置为fill_parent时不允许它可见
  • @Pztar,我把recyclerview改成wrap_content,还是看不到进度条。
  • 你不能那样做。将您的RefreshLayout 设置为像300dp 这样的设置高度,然后您会看到进度条
  • @Pztar 将其设置为硬编码数字是没有用的,也不是我想要的。有没有办法在recyclerview上显示进度条??
  • 另外作为旁注 fill_parent 已弃用,使用 match_parent 代替它做同样的事情,不要使用 px 值使用 dp

标签: android android-fragments progress-bar


【解决方案1】:

如果您将根布局更改为RelativeLayout,那么它相当简单:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25dp"
android:minHeight="25dp">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/latestnews_swipe_refresh_layout"
    android:layout_above="@id/loadmore_progressBar"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView_latestnews"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>

<ProgressBar
    android:id="@+id/loadmore_progressBar"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:indeterminate="true"
    android:visibility="visible"
    android:layout_alignParentBottom="true"
    android:layout_centerInParent="true"/>
</RelativeLayout>

不过,在我看来,我会通过适配器将 ProgressBar 放在 RecyclerView 中,它看起来要好得多,但选择权是你的。

【讨论】:

    【解决方案2】:

    RecyclerView 的高度不能设置为question 中所述的 wrap_content,因此它没有留出任何空间来显示ProgressBar。您需要设置一个自定义 LayoutManager 来执行此操作。

    other question 中也有一些示例,它似乎只是解决了您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多