Swiperefreshlayout不刷新布局

时间:2017-07-08 06:46:43

标签: android android-layout

swiperefreshlayout无法使用不同视图。我创建了主要布局,并在其中包含了一个布局,如代码所示。

 <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar">


            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_post_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                >

            </android.support.v7.widget.RecyclerView>

            <include layout="@layout/layout_util" />
        </RelativeLayout>
    </android.support.v4.widget.SwipeRefreshLayout>

1 个答案:

答案 0 :(得分:0)

SwipeRefreshLayout仅支持单个if ( !class_exists( 'WP_Theme_Handler_Checker' ) { //The existing class declaration } ListView子级。 Official src

所以你可以尝试SwipeRefreshMultipleViews

编辑:

首先在MultiSwipeRefreshLayout的自定义类上创建

GridView

然后改变你的布局,

public class MultiSwipeRefreshLayout extends SwipeRefreshLayout {

    private View[] mSwipeableChildren;

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

    public MultiSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * Set the children which can trigger a refresh by swiping down when they are visible. These
     * views need to be a descendant of this view.
     */
    public void setSwipeableChildren(final int... ids) {
        assert ids != null;

        // Iterate through the ids and find the Views
        mSwipeableChildren = new View[ids.length];
        for (int i = 0; i < ids.length; i++) {
            mSwipeableChildren[i] = findViewById(ids[i]);
        }
    }

    // BEGIN_INCLUDE(can_child_scroll_up)

    /**
     * This method controls when the swipe-to-refresh gesture is triggered. By returning false here
     * we are signifying that the view is in a state where a refresh gesture can start.
     * <p>
     * <p>As {@link SwipeRefreshLayout} only supports one direct child by
     * default, we need to manually iterate through our swipeable children to see if any are in a
     * state to trigger the gesture. If so we return false to start the gesture.
     */
    @Override
    public boolean canChildScrollUp() {
        if (mSwipeableChildren != null && mSwipeableChildren.length > 0) {
            // Iterate through the scrollable children and check if any of them can not scroll up
            for (View view : mSwipeableChildren) {
                if (view != null && view.isShown() && !canViewScrollUp(view)) {
                    // If the view is shown, and can not scroll upwards, return false and start the
                    // gesture.
                    return false;
                }
            }
        }
        return true;
    }
    // END_INCLUDE(can_child_scroll_up)

    // BEGIN_INCLUDE(can_view_scroll_up)

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

注意:用你的。更改in.muthu.stackoverflow.ui.widget   包裹的MultiSwipeRefreshLayout

在你的java代码中你应该这样做,

<?xml version="1.0" encoding="utf-8"?>
<in.muthu.stackoverflow.ui.widget.MultiSwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <GridView
                android:id="@+id/contact_screen_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:numColumns="2" />

            <TextView
                android:id="@android:id/empty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/empty_text"
                android:layout_gravity="center"/>

        </FrameLayout>

    </FrameLayout>
</in.muthu.stackoverflow.ui.widget.MultiSwipeRefreshLayout>