我想用ListVIEw实现滚动刷新功能.此外,如果列表为空,则在同一布局文件中还有其他视图元素.这是我的布局文件.问题是,当我向下滚动然后尝试向上滚动时,而不是一直滚动到顶部然后刷新它只是在那里刷新并向上滚动不起作用.
<androID.support.v4.Widget.SwipeRefreshLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:ID="@+ID/swipe_container" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" ><linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" > <relativeLayout androID:layout_wIDth="wrap_content" androID:layout_height="64dp" androID:paddingleft="16dp" androID:paddingRight="16dp" > <ImageVIEw androID:layout_wIDth="40dp" androID:layout_height="40dp" androID:layout_alignParentleft="true" androID:layout_centerVertical="true" androID:src="@drawable/inBox_empty" /> <TextVIEw androID:ID="@+ID/noEventsText" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerVertical="true" androID:layout_marginleft="16dp" androID:layout_toRightOf="@+ID/noEventsIcon" /> <VIEw androID:ID="@+ID/divIDer" androID:layout_wIDth="match_parent" androID:layout_height="1px" androID:layout_alignParentBottom="true" androID:layout_marginleft="4dp" androID:layout_marginRight="4dp" androID:background="@color/divIDerOnBlack" /> </relativeLayout> <ListVIEw androID:ID="@+ID/List_items" androID:layout_wIDth="match_parent" androID:layout_height="fill_parent" androID:cachecolorHint="@androID:color/transparent" androID:choiceMode="multipleChoice" androID:descendantFocusability="afterDescendants" androID:divIDer="@androID:color/transparent" androID:divIDerHeight="0px" androID:scrollbars="none" /></linearLayout></androID.support.v4.Widget.SwipeRefreshLayout>
这是我的onScroll方法.
@OverrIDepublic voID onScroll(AbsListVIEw vIEw,int firstVisibleItem,int visibleItemCount,int totalitemCount) { // If the total item count is zero and the prevIoUs isn't, assume the // List is invalIDated and should be reset back to initial state if (totalitemCount < prevIoUsTotalitemCount) { this.currentPage = this.startingPageIndex; this.prevIoUsTotalitemCount = totalitemCount; if (totalitemCount == 0) { this.loading = true; } } // If it's still loading, we check to see if the dataset count has // changed, if so we conclude it has finished loading and update the current page // number and total item count. if (loading && (totalitemCount > prevIoUsTotalitemCount)) { loading = false; prevIoUsTotalitemCount = totalitemCount; currentPage++; } // If reverse then the firstVisibleItem is calculated wrong if (reverse) { firstVisibleItem = totalitemCount - firstVisibleItem; } // If it isn't currently loading, we check to see if we have breached // the visibleThreshold and need to reload more data. // If we do need to reload some more data, we execute onl oadMore to fetch the data. if (!loading && (totalitemCount - visibleItemCount)<=(firstVisibleItem + visibleThreshold)) { onl oadMore(currentPage + 1, totalitemCount); loading = true; }}
解决方法:
为了使SwipeRefreshLayout工作,它需要是ListVIEw的直接父级,ListVIEw应该是SwipeRefreshLayout的第一个活动子视图.
SwipeRefreshLayout的文档说ListVIEw应该是唯一的子节点,但只要ListVIEw是第一个,它就可以有多个子节点.这意味着,例如,如果您使用视图为“empty”的适配器,SwipeRefreshLayout将正常工作.例如:
<androID.support.v4.Widget.SwipeRefreshLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:ID="@+ID/swipe_refresh" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context=".NearbyJobsActivity$PlaceholderFragment"> <ListVIEw androID:ID="@androID:ID/List" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:divIDer="@color/List_divIDer" androID:divIDerHeight="1dp" androID:ListSelector="@drawable/List_row_selector" /> <TextVIEw androID:ID="@androID:ID/empty" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:gravity="center" /></androID.support.v4.Widget.SwipeRefreshLayout>
如果您可以管理这种布局,那么SwipeRefreshLayout将正常工作,您将不需要在其他答案中列出的任何变通方法.
我自己的问题是我将ListVIEw作为片段加载,所以我实际上有:
<SwipeRefreshLayout> <FrameLayout> ) <ListVIEw/> \ fragment <TextVIEw/> / </FrameLayout> )</SwipeRefreshLayout>
所以SwipeRefreshLayout选择FrameLayout作为它的“目标”,它的默认canChildScrollUp()实现总是返回false.一旦我在Fragment中移动SwipeRefreshLayout,一切都开始正常工作.
<FrameLayout> <SwipeRefreshLayout> ) <ListVIEw/> \ fragment <TextVIEw/> / </SwipeRefreshLayout> )</FrameLayout>
总结 以上是内存溢出为你收集整理的java – 向上滚动不适用于Listview中的SwipeRefreshLayout全部内容,希望文章能够帮你解决java – 向上滚动不适用于Listview中的SwipeRefreshLayout所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)