gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:50:44 25 4
gpt4 key购买 nike

我正在尝试在我的一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com