我有以下问题:我有很长的 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/