SwipeRefreshLayout会干扰ViewPager中的GridView [英] SwipeRefreshLayout interfere with GridView inside ViewPager
问题描述
对于主布局,我有一个 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
时,会在列表达到顶部之前触发。 我如何解决这个问题?
)。
因此,您可以从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 =@
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/>
xmlns:android =http://schemas.android.com/apk/res/android
xmlns:
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_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(上下文上下文){
super(context);
mSwipeableChildren =新的GridView [3];
public MultiSwipeRefreshLayout(Context context,AttributeSet attrs){
super(context,attrs);
mSwipeableChildren =新的GridView [3];
}
public void addGridView(GridView v,int i)
{
mSwipeableChildren [i] = v;
}
/ **
*此方法控制触发刷新手势的时间。通过在这里返回false
*我们表示视图处于刷新姿态可以开始的状态。
*
*< p>由于{@link android.support.v4.widget.SwipeRefreshLayout}仅支持
* default的一个直接子元素,因此我们需要手动迭代可滑动的子元素看看是否有任何处于
*状态来触发该手势。如果是这样,我们返回false来开始手势。
* /
@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;
}
}
}
返回true;
}
/ **
*检查{@link View}是否可以从当前位置向上滚动的实用方法。
*处理平台版本差异,提供
*所需的向后兼容功能。
* /
private static boolean canViewScrollUp(GridView视图)
{
返回view.getChildCount()> 0&&
(view.getFirstVisiblePosition()> 0
|| view.getChildAt(0).getTop()< view.getPaddingTop());
}
public void setVisibleView(int i)
{
visibleView = i;
}
}
i have a ViewPager
for the main layout, with 3 fragment as pages, 2 of them has a GrideView
inside them.
the main page code:
<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>
code of first fragment:
<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>
the grid view is filled with items. in the middle of list when i scroll upward the SwipeRefreshLayout
get triggered before the list reach top. how can i solve this?
i hate answering my own question, but i always do:).
so from an example of google SwipeRefreshMultipleViews, with a little change in class MultiSwipeRefreshLayout
i managed to solve the problem.
<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>
the java part
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;
}
}
这篇关于SwipeRefreshLayout会干扰ViewPager中的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!