简体   繁体   English

scrollview android内的recyclerview滚动事件

[英]scroll event for recyclerview inside scrollview android

I have 2 horizontal and 1 vertical (Grid layout) in a single Scrollview. 我在一个Scrollview中有2个水平和1个垂直(网格布局)。

<ScrollView>

<Horizontal RecyclerView/>

<Horizontal RecyclerView/>

<Vertical RecyclerView/>

</ScrollView>

Above is the schematic of my view. 以上是我的观点的示意图。

I have to load data on once the vertical recyclerviews last item is visible, but i'm unable to get the scroll event for my vertical view. 我必须在垂直recyclerviews最后一项可见时加载数据,但我无法获得垂直视图的滚动事件。

Here is my scrollviewlistener. 这是我的scrollviewlistener。

             recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);

                    totalItemCount = gridLayoutManager.getItemCount();
                    lastVisibleItem = gridLayoutManager.findLastVisibleItemPosition();
                    if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                        // End has been reached
                        // Do something
                        if(onLoadMoreListener!=null) {
                            onLoadMoreListener.onLoadMore(lastVisibleItem);
                        }
                        loading = true;
                    }
                }
            });

but this setonscrollListener is never fired. 但是这个setonscrollListener永远不会被触发。 How do I get scroll event for above case. 如何获得上述案例的滚动事件。

Thanks in advance:) 提前致谢:)

I had the problem here is how to solve it: 我遇到的问题是如何解决它:

  1. First create an Interface for scroll listener 首先为滚动侦听器创建一个接口

     public interface EndlessScrollListener { void onScrollChanged(EndlessScrollView scrollView,int x, int y, int oldx, int oldy); } 
  2. Create a Custom ScrollView by extending the ScrollView 通过扩展ScrollView创建自定义ScrollView

     public class EndlessScrollView extends ScrollView { private EndlessScrollListener endlessScrollListener = null; public EndlessScrollView(Context context) { super(context); } public EndlessScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public EndlessScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public void setScrollViewListener(EndlessScrollListener endlessScrollListener) { this.endlessScrollListener = endlessScrollListener; } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (endlessScrollListener != null) { endlessScrollListener.onScrollChanged(this, l, t, oldl, oldt); } } } 
  3. Then in your Fragment/Activity layout use the EndlessScrollView instead of Default one: 然后在Fragment / Activity布局中使用EndlessScrollView而不是Default one:

     <YOUR_PACKAGE_NAME.EndlessScrollView android:id="@+id/my_scroll_view"> <!-- your recycler views ... --> </YOUR_PACKAGE_NAME.EndlessScrollView> 
  4. Next your Activity/Fragment should implement the Interface your created in step 1. 接下来,您的Activity / Fragment应该实现您在步骤1中创建的接口。

  5. set your Activity/Fragment as Scroll Listener and then use like this: 将您的Activity / Fragment设置为Scroll Listener,然后像这样使用:

     public class YourActivity extends Activity implements EndlessScrollListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EndlessScrollView myScrollView = (EndlessScrollView) findViewById(R.id.my_scroll_view); myScrollView.setScrollViewListener(this); } @Override public void onScrollChanged(EndlessScrollView scrollView, int x, int y, int oldx, int oldy) { // We take the last son in the scrollview View view = scrollView.getChildAt(scrollView.getChildCount() - 1); int distanceToEnd = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY())); // if diff is zero, then the bottom has been reached if (distanceToEnd == 0) { // do stuff your load more stuff } } } 

Use the gesture detector to get the scroll event in RecyclerView. 使用手势检测器在RecyclerView中获取滚动事件。

EDIT Replacing ScrollView with NestedScrollView solved my problem of recyclerview scrolling. 编辑用NestedScrollView替换ScrollView解决了我的recyclerview滚动问题。

final NestedScrollView parentScrollView=(NestedScrollView)view.findViewById (R.id.parentScrollView);
            parentScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
                    @Override
                    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                        //    Log.d("ScrollView","scrollX_"+scrollX+"_scrollY_"+scrollY+"_oldScrollX_"+oldScrollX+"_oldScrollY_"+oldScrollY);
                            //Do something
                            }
                    }
            });

when you use tow scrollble element inside each other you are in hot water! 当你在彼此内部使用拖动滚动元素时,你处于热水中! you should calculate the Recycler item height and then find the whole recycler height. 您应该计算Recycler项目的高度,然后找到整个回收者的高度。 look at below link, I explain completely this problem. 看下面的链接,我完全解释了这个问题。

Use RecyclerView indie ScrollView with flexible recycler item height 使用RecyclerView独立ScrollView,灵活的回收物项目高度

I hope it help you 我希望它对你有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM