RecyclerView 后 ProgressBar 不可见
ProgressBar is not visible after RecyclerView
我想在使用 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>
片段已加载到 viewpager 中:
<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
的高度不能设置为 wrap_content,如 question 中所述,因此它没有留出任何 space 来显示ProgressBar
。您需要设置自定义 LayoutManager
才能执行此操作。
你在这个 other question 中也有一些例子,它似乎正好解决了你的问题。
如果您将根布局更改为 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 中,它看起来好多了,但选择权在你。
我想在使用 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>
片段已加载到 viewpager 中:
<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
的高度不能设置为 wrap_content,如 question 中所述,因此它没有留出任何 space 来显示ProgressBar
。您需要设置自定义 LayoutManager
才能执行此操作。
你在这个 other question 中也有一些例子,它似乎正好解决了你的问题。
如果您将根布局更改为 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 中,它看起来好多了,但选择权在你。