android - 如何同时启用 SwipeRefreshLayout 和初始加载 ListView ProgressBar

标签 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 位于如图所示的相对布局之上,我在初始加载时不会得​​到旋转的圆圈,但会得到我的 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/

相关文章:

android - 静态共享首选项

java - 包含多次使用 View 的自定义 xml 相对布局

java - java中不支持的类版本错误?

xamarin - 我们如何处理 Xamarin Forms Picker 的完成按钮单击事件?

xamarin.ios - 如何在iOS中使用 'Container View'? ( Storyboard)

android - keystore 文件不存在

android - Dagger 2 无法访问 Retrofit

android - RemoteException 发送 setActive(false) 通知 - 随机 Activity 关闭

c# - Xamarin Forms ListView 不显示 TextCells

c# - Xamarin Forms 异步播放声音