【问题标题】:SwipeRefreshLayout interfere with GridView inside ViewPagerSwipeRefreshLayout 干扰 ViewPager 中的 GridView
【发布时间】:2015-01-11 09:24:14
【问题描述】:

我有一个ViewPager 用于主布局,有 3 个片段作为页面,其中 2 个在其中有一个 GrideView

主页代码:

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

      <android.support.v4.view.ViewPager
        android:id="@+id/main_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      </android.support.v4.view.ViewPager>
</android.support.v4.widget.SwipeRefreshLayout>

第一个片段的代码:

<GridView 
    android:id="@+id/firstCategoryGridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="1"
    android:stretchMode="columnWidth"
    android:scrollbars="none"
    android:listSelector="#00000000">
</GridView>

网格视图充满了项目。在列表中间,当我向上滚动时,SwipeRefreshLayout 在列表到达顶部之前被触发。 我该如何解决这个问题?

【问题讨论】:

    标签: android gridview android-viewpager swiperefreshlayout


    【解决方案1】:

    我讨厌回答自己的问题,但我总是这样做:)。

    因此,从 google SwipeRefreshMultipleViews 的示例中,MultiSwipeRefreshLayout 的类稍有变化,我设法解决了问题。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black">
    
        <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/dark_blue"
            app:theme="@style/Theme.AppCompat"
            android:layout_alignParentTop="true"/>
    
        <com.mehdok.slidingtabs.SlidingTabLayout
            android:id="@+id/main_sliding_tabs"
            android:background="@color/blue"   
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/main_toolbar" />
    
        <fr.castorflex.android.smoothprogressbar.SmoothProgressBar
            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="4dp"
            android:indeterminate="true"
                app:spb_sections_count="4"
            app:spb_color="@color/white"
                app:spb_speed="0.5"
            app:spb_stroke_width="4dp"
                app:spb_stroke_separator_length="4dp"
            app:spb_reversed="false"
                app:spb_mirror_mode="false"
            app:spb_progressiveStart_activated="false"
                app:spb_progressiveStart_speed="1.5"
            app:spb_progressiveStop_speed="3.4"
            android:layout_below="@id/main_toolbar"
            android:visibility="gone"
            android:id="@+id/downloadBar"/>
    
        <com.mehdok.views.MultiSwipeRefreshLayout
            android:id="@+id/swiperefresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/main_sliding_tabs">
    
        <android.support.v4.view.ViewPager
                    android:id="@+id/main_pager"
                    android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
            </android.support.v4.view.ViewPager>
            </com.mehdok.views.MultiSwipeRefreshLayout>
    
    </RelativeLayout>
    

    java部分

    public class MultiSwipeRefreshLayout extends SwipeRefreshLayout {
    
        private GridView[] mSwipeableChildren;
        private int visibleView = 0;
    
        public MultiSwipeRefreshLayout(Context context) {
            super(context);
            mSwipeableChildren = new GridView[3];
        }
    
        public MultiSwipeRefreshLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            mSwipeableChildren = new GridView[3];
        }
    
    
        public void addGridView(GridView v, int i)
        {
            mSwipeableChildren[i] = v;
        }
    
    
        /**
         * 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>As {@link android.support.v4.widget.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) {
                    for(int i = 0; i < 3; i++)
                    {
                            if((i == visibleView) && mSwipeableChildren[i] != null && !canViewScrollUp(mSwipeableChildren[i]))
                            {
                                    return false;
                            }
                    }               
            }
    
            return true;
        }
    
    
    
        /**
         * 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(GridView view)
        {
            return view.getChildCount() > 0 &&
                    (view.getFirstVisiblePosition() > 0
                            || view.getChildAt(0).getTop() < view.getPaddingTop());
        }
    
        public void setVisibleView(int i)
        {
            visibleView = i;
        }
    
    }
    

    【讨论】:

    • 你能告诉我,你是如何用 MultiSwipeRefreshLayout 类解决这个问题的,我被这个问题困住了,无法解决。
    • @Lakshmanan 已经是一年前了,实际上我不记得我做了什么,但我将我的代码添加到了答案中。也许你可以从中提取一些东西。
    • 感谢您的回答,此外我尝试了您的代码,但是现在,即使网格视图达到顶部,SwipeRefreshLayout 也没有触发,让我尝试其他方法,如果您有任何想法,请告诉我。跨度>
    • MultiSwipeRefreshLayout 内我有一个方法addGridView。您必须调用它才能将视图添加到刷新机制。我有 3 个 GridView 所以在 MultiSwipeRefreshLayout 我创建了一个 3 个数组。根据需要修改它并调用 addGridView
    • 是的,我正在调用 addGridView,我在 ViewPager 上也有 3 个 GridView,问题是,滚动工作正常,但最后在 GridView 顶部后,当我向下滑动时,SwipeRefreshLayout不是触发器。
    【解决方案2】:

    使用新类扩展 SwipeRefreshLayout 并覆盖其 canChildScrollUp()。当您想要向下滚动查看 gridview 时返回 true。例如

    @override.
    boolean canChildScrollUp()
    {
    return gridview.getFirstVisibleItemPostion()!=0;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-09-22
      • 2015-02-12
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多