选项卡式视图中的 SwipeRefreshLayout
SwipeRefreshLayout in Tabbed View
我正在尝试在我的选项卡式视图上应用 SwipeRefrshLayout。
这是我想要 swipeRefreshLayout 的选项卡片段的布局文件
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:divider="@null" />
</ScrollView>
这是 activity
的布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/action_bar"
style="@style/Widget.MaterialSheetFab.ToolBar" />
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
style="@style/Widget.MaterialSheetFab.TabLayout" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_below="@+id/appbar"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
</RelativeLayout>
片段文件中的代码:
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (SuperAwesomeCardFragment.listAchievements.getChildAt(0) != null) {
StudentInfoMainActivity.swipeRefreshLayout.setEnabled(listAchievements.getFirstVisiblePosition() == 0 && listAchievements.getChildAt(0).getTop() == 0);
}
}
应用 google 示例代码中显示的上述布局和代码示例后,我成功实现了 swiperefreshllayout。但是存在无法向下滚动的问题,因为滑动覆盖了它。根据堆栈溢出线程 ,我添加了解决问题的代码。但这又增加了一个问题。
列表第一行的高度比我的屏幕尺寸大的场景,然后列表只显示第一个元素。其他元素不显示在屏幕上。只有当行中的第一个元素更大时才会发生这种情况。如果我添加另一行高度小于或等于屏幕大小,则所有元素都将正确显示。
我想强调的另一点是,如果我在 activity(而不是 Tabs 或 Fragments)中使用类似的代码,它会按预期完美运行,并且不会遇到以下问题。
这有点奇怪。
Image 1
Image 2
我添加了两张图片:
Image1: 在这张图片中,列表视图中的第一个元素的高度小于屏幕高度,因此列表视图中的所有元素在上下滚动时都是可见的。
Image2:这里我刚刚从图像 1 中删除了列表视图的第一个元素。所以看到的照片现在是列表视图中的第一个元素。在这种情况下,如图像所示,元素(图像+填充和所有)的高度大于屏幕尺寸(高度)。现在,当我尝试向下滚动时,滚动只会持续到图像显示完整为止。向下滚动时,我无法在列表视图中看到任何其他行。
更新:
多次在 Logcat 中获取以下行
10-30 08:56:02.851 19665-19665/com.malav.holistree E/SwipeRefreshLayout: Got ACTION_MOVE event but don't have an active pointer id.
发布答案,因为它可以帮助其他人。
使用 ScrollView 实际上是在制造问题。
如@jonathanrz 所述,将 Linearlayout 与 RecyclerView 一起使用。
效果不错..
我正在尝试在我的选项卡式视图上应用 SwipeRefrshLayout。
这是我想要 swipeRefreshLayout 的选项卡片段的布局文件
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:divider="@null" />
</ScrollView>
这是 activity
的布局文件<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/action_bar"
style="@style/Widget.MaterialSheetFab.ToolBar" />
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
style="@style/Widget.MaterialSheetFab.TabLayout" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_below="@+id/appbar"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
</RelativeLayout>
片段文件中的代码:
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (SuperAwesomeCardFragment.listAchievements.getChildAt(0) != null) {
StudentInfoMainActivity.swipeRefreshLayout.setEnabled(listAchievements.getFirstVisiblePosition() == 0 && listAchievements.getChildAt(0).getTop() == 0);
}
}
应用 google 示例代码中显示的上述布局和代码示例后,我成功实现了 swiperefreshllayout。但是存在无法向下滚动的问题,因为滑动覆盖了它。根据堆栈溢出线程 ,我添加了解决问题的代码。但这又增加了一个问题。 列表第一行的高度比我的屏幕尺寸大的场景,然后列表只显示第一个元素。其他元素不显示在屏幕上。只有当行中的第一个元素更大时才会发生这种情况。如果我添加另一行高度小于或等于屏幕大小,则所有元素都将正确显示。
我想强调的另一点是,如果我在 activity(而不是 Tabs 或 Fragments)中使用类似的代码,它会按预期完美运行,并且不会遇到以下问题。 这有点奇怪。
Image 1
Image 2
我添加了两张图片: Image1: 在这张图片中,列表视图中的第一个元素的高度小于屏幕高度,因此列表视图中的所有元素在上下滚动时都是可见的。
Image2:这里我刚刚从图像 1 中删除了列表视图的第一个元素。所以看到的照片现在是列表视图中的第一个元素。在这种情况下,如图像所示,元素(图像+填充和所有)的高度大于屏幕尺寸(高度)。现在,当我尝试向下滚动时,滚动只会持续到图像显示完整为止。向下滚动时,我无法在列表视图中看到任何其他行。
更新: 多次在 Logcat 中获取以下行
10-30 08:56:02.851 19665-19665/com.malav.holistree E/SwipeRefreshLayout: Got ACTION_MOVE event but don't have an active pointer id.
发布答案,因为它可以帮助其他人。 使用 ScrollView 实际上是在制造问题。 如@jonathanrz 所述,将 Linearlayout 与 RecyclerView 一起使用。 效果不错..