如何启用SwipeRefreshLayout并同时初始加载ListView ProgressBar

时间:2016-07-04 18:28:52

标签: android xamarin

我有以下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 / refresh。

但是,如果我将RelativeLayout更改为高于Listview,我会在加载时获得旋转圆圈,但我的Listview后面没有显示?

关于活动的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;
        }

1 个答案:

答案 0 :(得分:3)

根据android docs SwipeRefreshLayout只能托管一个孩子。结果,第二个视图始终被隐藏。要使其工作,您必须将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>