Android SwipeRefresh布局不使用自定义空listview
我试图在我的一个片段中添加一个Android SwipeRefresh布局不使用自定义空listview,android,listview,android-listfragment,empty-list,swiperefreshlayout,Android,Listview,Android Listfragment,Empty List,Swiperefreshlayout,我试图在我的一个片段中添加一个SwipeRefreshLayout布局。 当列表包含一些元素时,一切都会按预期进行 问题是当我的列表为空时。当列表不包含任何项目时,我会显示一个ViewStub,此时我的OnRefreshListener从未被触发 以下是我的ListFragment xml: <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipeRenewals" android:layout
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>