选项卡式视图中的SwipeRefreshLayout

时间:2016-10-29 20:18:14

标签: android listview android-fragments swiperefreshlayout

我正在尝试在标签视图中应用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>

这是活动的布局文件

<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);
    }

}

通过谷歌应用示例代码中显示的上述布局和代码示例后,我成功地实现了swiperefreshllayout。但是有一个问题是无法向下滚动,因为刷卡覆盖了它。根据堆栈溢出https://stackoverflow.com/a/35779571/4180170上的线程,我添加了解决我的问题的代码。但它又增加了一个问题。 列表的第一行的高度大于我的屏幕大小的情况,然后列表仅显示第一个元素。其他元素未显示在屏幕上。仅当行中的第一个元素更大时才会发生这种情况。如果我添加另一行高度小于或等于屏幕大小的行,则正确显示所有元素。

我想强调的另一点是,如果我在活动中使用类似的代码(而不是Tabs或Fragments),而不是按预期完美地工作,并且不会遇到以下问题。 这有点奇怪。

Image 1

Image 2

我添加了两张图片: Image1:在此图像中,列表视图中的第一个元素的高度小于屏幕高度,因此listView中的所有元素在向上和向下滚动时都可见。

Image2:这里我刚从图像1中删除了列表视图的第一个元素,因此所看到的照片是listview中现在的第一个元素。在这种情况下,如图所示,元素的高度(图像+填充和全部)大于屏幕尺寸(高度)。现在,当我尝试向下滚动时,滚动仅直到图像看到已满。在向下滚动时,我无法在列表视图中看到任何其他行。

更新: 多次在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.

1 个答案:

答案 0 :(得分:0)

发布答案,因为它可以帮助其他人。 使用ScrollView实际上是在创建问题。 如@jonathanrz所述,使用Linearlayout和RecyclerView。 效果很好..