[英]recyclerview inside nestedscrollview addOnScrollListener (endless scrolllistener)
I use recyclerview inside a nestedscrollview as follows:我在嵌套滚动视图中使用 recyclerview 如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
android:id="@+id/mainScrollView"
android:layout_marginBottom="?attr/actionBarSize"
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">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:overScrollMode="never"
android:layout_height="250dp"
android:focusableInTouchMode="true"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/VerticalRV"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
The problem is that when I want to set a listener ( onScrollListener
) to this recycler view, it doesn't get working anyway.问题是,当我想为这个回收器视图设置一个监听器( onScrollListener
)时,它无论如何都无法工作。 I also have debugged this piece of code, but it doesn't even catch the event.我也调试了这段代码,但它甚至没有捕捉到事件。 Below is the java code:下面是java代码:
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
MyAdapter adapter = new MyAdapter(verticalShownData, this.getActivity());
recyclerView.setAdapter(adapter);
recyclerView.addOnScrollListener(new HideShowScrollListener() {
@Override
public void onHide() {
animateCallback.animateHide();
}
@Override
public void onShow() {
animateCallback.animateShow();
}
});
How can I get this listener working?我怎样才能让这个听众工作? Thanks in advance.提前致谢。
I know this might be little late but maybe this will help someone.我知道这可能有点晚了,但也许这会对某人有所帮助。 I was also struck in a problem where my recyclerview keeps calling onScrolled method of RecyclerView as soon as data is inserted into RecyclerView.我还遇到了一个问题,即一旦数据插入 RecyclerView,我的 recyclerview 就会不断调用 RecyclerView 的 onScrolled 方法。 After some debugging I found that, I'm using NestedScrollView with RecyclerView.经过一些调试后,我发现,我将 NestedScrollView 与 RecyclerView 一起使用。 So I searched for something related to it and I found a great medium.com tutorial here .所以我搜索了一些与它相关的东西,我在这里找到了一个很棒的medium.com教程。 It works pretty fine.它工作得很好。
Adding some code from link here.从这里的链接添加一些代码。
mNestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if(v.getChildAt(v.getChildCount() - 1) != null) {
if ((scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) &&
scrollY > oldScrollY) {
int visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
int pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
if (!isLoadingData()) {
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
//Load Your Data
}
}
}
}
}
});
After a couple of days, I finally come up with a solution.几天后,我终于想出了一个解决方案。
The solution is removing the NestedScrollView
and instead using RecyclerView
with header.解决方案是删除NestedScrollView
,而是使用带有标题的RecyclerView
。 So the first element of the RecyclerView
is a ViewPager
and the rest are other objects.所以RecyclerView
的第一个元素是一个ViewPager
,其余的是其他对象。
I think that using NestedScrollView
as parent of RecyclerView
is not such a good practice.我认为使用NestedScrollView
作为RecyclerView
父级并不是一个好习惯。 Since NestedScrollView
forces its child to be fully loaded and this is against the concept of RecyclerView
.由于NestedScrollView
强制其子项完全加载,这违反了RecyclerView
的概念。
The solution is inspired by @hister's answer on this thread .该解决方案的灵感来自@hister 在此线程上的回答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.