外部 Recyclerview 没有收到内部 Recyclerview 的滚动事件 [英] Outer Recyclerview not receiving scroll events of inner Recyclerview

查看:26
本文介绍了外部 Recyclerview 没有收到内部 Recyclerview 的滚动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照本教程实现了滚动时隐藏工具栏和 FAB 的行为:

现在,我对标签中的回收视图中的那些单独的项目进行了编码,而不是仅包含一个 textView,因此它们包含一个图片 (ImageView),并在其下方显示一个显示项目列表的回收视图.

因此,有一个包含内部 recyclerview 列表的外部 recyclerview.

内部 recyclerview 不滚动 - 我通过重写 canScrollVertically() 方法按照此线程中的答案禁用了它:在子 Recyclerview android 中禁用滚动.我也尝试启用内部 recyclerview 的滚动,但我遇到了同样的问题.

外部回收视图确实滚动并具有显示/隐藏工具栏和 FAB 的行为.

当我按住图片 (ImageView) 滚动时,应用程序的行为运行良好,显示和隐藏工具栏和 FAB.但是,当我将手指放在内部 recyclerview 上滚动时,外部 recyclerview 滚动并且列表上下移动,但是显示/隐藏工具栏和 FAB 的行为永远不会被激活.

我有一种感觉,这是因为内部recyclerview拦截了scroll,而外部recyclerview没有得到scroll事件来激活行为.

有谁知道如何确保外部 recyclerview 也获取滚动事件以便行为正常工作?

解决方案

Hank Moody 的评论实际上让我找到了正确的答案 - 谢谢 Hank!

这是我解决问题的方法:

  1. 创建一个滚动"回收器视图,父级将通过执行以下操作接收子级的所有滚动事件:

    public class ScrollThroughRecyclerView extends RecyclerView {公共 ScrollThroughRecyclerView(上下文上下文){超级(上下文);}公共 ScrollThroughRecyclerView(上下文上下文,AttributeSet attrs){超级(上下文,属性);}公共 ScrollThroughRecyclerView(上下文上下文,AttributeSet attrs,int defStyle){超级(上下文,属性,defStyle);}@覆盖公共布尔 dispatchTouchEvent(MotionEvent ev){//true - 阻止滚动子级并允许父级回收器滚动返回真;}}

  2. 在您的 xml 或 Java 代码中使用此自定义回收器视图,滚动事件将正确传递到您的父回收器视图,这将激活应用程序滚动行为.

I followed this tutorial to implement the behaviors for both hiding the toolbar and the FAB when scrolled: https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling(part3)/

I have pasted a demo of what the behavior looks like below.

Now instead of those individual items within the recyclerview in the tabs holding just a textView, I have coded it so that they hold a picture (ImageView) and below it, a recyclerview showing a list of items.

Hence, there is an outer recyclerview that contains a list of inner recyclerviews.

The inner recyclerview does not scroll - I disabled it by following the answer in this thread by overriding the method canScrollVertically(): Disable Scrolling in child Recyclerview android. I also tried enabling the scrolling for the inner recyclerview, but I experienced the same problem.

The outer recyclerview does scroll and has the behavior that shows/hides the toolbar and the FAB.

When I scroll by holding onto the picture (ImageView), the app behavior works perfectly fine, showing and hiding the toolbar and FAB. However, when I have my finger on the inner recyclerview to scroll, the outer recyclerview scrolls and the list moves up and down, but the behavior of show/hiding the toolbar and FAB is never activated.

I have a feeling that this is because the inner recyclerview had intercepted the scroll and the outer recyclerview did not get the scroll event to activate the behavior.

Does anyone know how to make sure the outer recyclerview also gets the scroll event so that the behavior works?

解决方案

Hank Moody comment actually lead me to the correct answer - thanks Hank!

This is how I solved my problem:

  1. Create a 'Scroll Through' recyclerview where the parent will receive all the scroll events of the child by doing this:

    public class ScrollThroughRecyclerView extends RecyclerView {
        public ScrollThroughRecyclerView(Context context) {
            super(context);
        }
    
        public ScrollThroughRecyclerView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ScrollThroughRecyclerView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public boolean dispatchTouchEvent(MotionEvent ev){
            //true - block scrolling of child and allow scrolling for parent recycler
            return true;
        }
    }
    

  2. Use this custom recyclerview in your xml or in your java code and the scroll events will be passed correctly to your parent recyclerview which will activate the app scrollbehavior.

这篇关于外部 Recyclerview 没有收到内部 Recyclerview 的滚动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆