java - ViewPager fragment 中的 SwipeToRefresh 布局问题

标签 java android listview android-fragments

所以,我有 ViewPager 的 Activity 和其中的 2 个 fragment 。在每个 fragment 中,我放置了 SwipeToRefreshLayout 和 ListView。 XML 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>

*<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/going_out_activity_swipe_to_refresh"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">*
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/going_out_activity_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_going_out"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_tab_new"/>


</RelativeLayout>


</android.support.v4.widget.SwipeRefreshLayout>

现在,有一个问题。当列表超出我的屏幕时,我必须向下滚动才能看到项目,当我尝试向上滚动时,它不会滚动,而是刷新(调用 SwipeToRefreshLayout )。我该如何解决这个问题?

最佳答案

取自 android docs :

他们使用ListFragment,但您应该能够进行必要的更改: 您应该添加并使用自定义 SwipeRefreshLayout,在其中定义滚动条件:

private class ListFragmentSwipeRefreshLayout extends SwipeRefreshLayout {

    public ListFragmentSwipeRefreshLayout(Context context) {
        super(context);
    }

    /**
     * As mentioned above, we need to override this method to properly signal when a
     * 'swipe-to-refresh' is possible.
     *
     * @return true if the {@link android.widget.ListView} is visible and can scroll up.
     */
    @Override
    public boolean canChildScrollUp() {
        final ListView listView = getListView();
        if (listView.getVisibility() == View.VISIBLE) {
            return canListViewScrollUp(listView);
        } else {
            return false;
        }
    }

}

/**
 * Utility method to check whether a {@link ListView} can scroll up from it's current position.
 * Handles platform version differences, providing backwards compatible functionality where
 * needed.
 */
private static boolean canListViewScrollUp(ListView listView) {
    if (android.os.Build.VERSION.SDK_INT >= 14) {
        // For ICS and above we can call canScrollVertically() to determine this
        return ViewCompat.canScrollVertically(listView, -1);
    } else {
        // Pre-ICS we need to manually check the first visible item and the child view's top
        // value
        return listView.getChildCount() > 0 &&
                (listView.getFirstVisiblePosition() > 0
                        || listView.getChildAt(0).getTop() < listView.getPaddingTop());
    }
}

关于java - ViewPager fragment 中的 SwipeToRefresh 布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36196753/

相关文章:

java - 使用Java发送电子邮件时如何防止调试代码?

java - Android:尝试向我的应用程序添加另一种语言后出现 Inflate 异常

android - 从 Phonegap : Protocol not supported 链接到 Google Play 应用

android - 带有图像列表的 ListView 无法在自定义适配器中正常工作以进行多项选择

android - 当我一次又一次滚动 ListView 时应用程序崩溃

java - JPA - 级联坚持一对一的关系

java - 通过 Java 代码检查进程是否在 linux 上运行

Android OpenGLES 棕色方 block

Android Firebase : retrieve All data in ArrayList and randomly select string from that ArrayList

java.lang.ClassCastException 适配器转换为 Activity