android - SwipeRefresh 布局不适用于自定义空 ListView

标签 android listview android-listfragment empty-list swiperefreshlayout

我正在尝试在我的 fragment 之一中添加 SwipeRefreshLayout 布局。 当列表包含一些元素时,一切都会按预期工作。

问题是当我的列表为空时。当列表不包含任何项目时,我会显示一个 ViewStub,并且此时我的 OnRefreshListener 永远不会被触发。

这是我的 ListFragment xml:

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/list"
            android:fadingEdge="vertical"
            android:layout_gravity="center"
            android:cacheColorHint="@android:color/transparent"/>
        <ViewStub
            android:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout="@layout/empty_renewal"/>
    </FrameLayout>

</android.support.v4.widget.SwipeRefreshLayout>

以及我在 ViewStub 中加载的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"/>
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_warning"
            android:adjustViewBounds="true"
            android:layout_weight="1" />
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"/>
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/no_renewal"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/refresh_for_new"/>

</LinearLayout>

那么有没有办法让列表为空时滑动手势触发刷新?

最佳答案

显然写下问题很有帮助。

我通过将 ViewStub 布局封装在 ScrollView 中解决了我的问题。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"/>
            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_warning"
                android:adjustViewBounds="true"
                android:layout_weight="1" />
            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"/>
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/no_renewal"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/refresh_for_new"/>

    </LinearLayout>
</ScrollView>

关于android - SwipeRefresh 布局不适用于自定义空 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23704993/

相关文章:

android - 无法运行openCV示例FaceDetection应用程序

android - arrayadapter.clear() 删除底层数据

Android - 在数据库中获取_id onItemClick

android - GetItem(int) 在 FragmentPagerAdapter 中返回 Listfragment

android - onCreate vs onCreateView 整合ActionBar-PullToRefresh

Android - 在横向和纵向模式之间切换使 Intent 失去值(value)

android - System.loadLibrary() 错误

android - 菜单未显示在第二个 Activity 中

android - 在 ListView 中使用通用图像加载器加载一次图像

android - 在 ListFragment 中没有事先关闭()的情况下完成游标