Android swiperefreshlayout + framelayout

标签 android

我有以下问题:我有很长的 ListView,但是当我向上滚动时会触发 onRefresh,而不是先滚动回顶部。我该如何解决这个问题?

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/splash_background">

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MyActivity"
        tools:ignore="MergeRootFrame"></FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>

我将以下 fragment 插入到 FrameLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/splash_background">
    <ListView
        android:id="@+id/files_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>

最佳答案

我不记得我是在哪里找到这个解决方案的,但我相信在某些情况下向上滚动时 SwipeRefreshLayout 会出现问题。我应用的解决方法是将 onScrollListener 添加到 ListView ,只要列表不在顶部位置,它就会启用和禁用滑动刷新。示例如下:

    listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView, int i) {}

        @Override
        public void onScroll(AbsListView absListView, int i, int i2, int i3) {
            int topRowVerticalPosition =
                    (absListView == null || absListView.getChildCount() == 0) ?
                            0 : absListView.getFirstVisiblePosition() == 0 ? absListView.getChildAt(0).getTop() : -1;
            swipeLayout.setEnabled(topRowVerticalPosition >= 0);
        }
    });

编辑:在 android 4.3 上遇到了这个问题。我已更新代码以反射(reflect)修复

关于Android swiperefreshlayout + framelayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27362806/

相关文章:

android - super 记录器 : not creating temp file or recording any audio

android - WebRTC。如何在 Android 通话期间禁用声音?

javascript - 如何解决 "exception firing resume/pause event from native"Cordova Android

android - 尝试使用Maven和/或Gradle将库导入android项目

android - 如何在 Android 和 iOS 上检查网络是否可用(Delphi XE5)

android - 使用 okhttp-signpost 改造 OAuth 签名获取特殊字符的 OAuthMessageSignerException

android - 屏幕外的图层列表形状

android - 如何使用大位图。旋转并插入画廊

java - 在动画期间更改 Z 顺序

android - 在 Android SDK 中设置 JAVA_HOME