ViewPager片段中的SwipeToRefresh布局问题

时间:2016-03-24 09:22:39

标签: java android listview android-fragments

所以,我有ViewPager和2个片段的活动。在每个片段中,我放置了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)。我该如何解决这个问题?

4 个答案:

答案 0 :(得分:1)

取自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());
    }
}

答案 1 :(得分:1)

ListViewSwipeToRefreshLayout一起使用时,这是一个错误。您可以使用RecyclerView代替ListView轻松修复它。

但是,如果您想继续使用ListView,请尝试使用以下代码:

mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                boolean enable = false;
                if (ptrListView != null && mListView.getChildCount() > 0) {
                    boolean firstItemVisible = (mListView.getFirstVisiblePosition() == 0);
                    boolean topOfFirstItemVisible = (mListView.getChildAt(0).getTop() == 0);
                    enable = firstItemVisible && topOfFirstItemVisible;
                }
                mSwipeRefreshLayout.setEnabled(enable);
            }
        });

答案 2 :(得分:0)

尝试仅将“滑动以刷新布局”添加到ListView中。

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

<RelativeLayout
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">

    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <ListView
        android:id="@+id/going_out_activity_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    </android.support.v4.widget.SwipeRefreshLayout>


    <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>

答案 3 :(得分:0)

这就是我做的方式,它完美滚动,只有当你一直向上滑动时才刷新。我希望这会有所帮助。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.newthinktank.listviewexample2.app.MainActivity"
android:background="@drawable/background2"
android:layout_marginTop="65dp"
android:weightSum="1">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipelayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/lv1"
        android:layout_marginTop="15dp"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:background="#f0f0f0"
        android:alpha="0.90"
        android:layout_weight="1.01" />

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

这就是我使用swipetorefresh的方式:

  mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipelayout);

    mSwipeRefreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    finish();
                    startActivity(getIntent());
                }
            }
    );