我有以下 main.axml 文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/refresher"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:id="@+id/myListView" />
<RelativeLayout
android:id="@+id/loadingPanel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
我需要能够在我的 ListView 上向下滑动来刷新,而且我还需要在初始加载时有一个“旋转的圆圈加载动画图标”。然而 如果 Listview 位于如图所示的相对布局之上,我在初始加载时不会得到旋转的圆圈,但会得到我的 Listview/刷新。
但是,如果我将 RelativeLayout 更改为高于 Listview,我会在加载时看到旋转的圆圈,但之后我的 Listview 没有显示?
在我关于 Activity 的 onCreate 方法中。
protected async override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
// Find view components
blogListView = FindViewById<ListView>(Resource.Id.myListView);
listViewRefresher = FindViewById<SwipeRefreshLayout>(Resource.Id.refresher);
loadingPanel = FindViewById<RelativeLayout>(Resource.Id.loadingPanel);
blogListView.Adapter = new FeedAdapter(this, _blogPosts);
//Register events
listViewRefresher.Refresh += HandleRefresh;
blogListView.ItemClick += OnListItemClick;
// Populate views
await PoplulateBlogPostsFromExternalWebUrl();
loadingPanel.Visibility = ViewStates.Gone;
}
最佳答案
根据android docs SwipeRefreshLayout 只能容纳一个 child 。因此,第二个 View 总是被隐藏。要使其正常工作,您必须将 ListView 和 RelativeLayout 包装在另一个 RelativeLayout 中。还值得注意的是,如果将 id 放在 ProgressBar 上,则可能会删除包装 ProgressBar 的 RelativeLayout。
OP 发现(在上面的评论中)这会导致在向上滚动列表时触发 SwipeRefreshLayout。 To address this you would need to disable the layout when the list view is not at the top .
示例代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/refresher"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/loadingContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:id="@+id/myListView" />
<RelativeLayout
android:id="@+id/loadingPanel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
</RelativeLayout>
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
关于android - 如何同时启用 SwipeRefreshLayout 和初始加载 ListView ProgressBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38190474/