外Recyclerview没有收到内部Recyclerview的滚动事件事件、Recyclerview

2023-09-05 03:00:09 作者:喝可乐的猫

我跟着这个教程实施的行为为隐藏工具栏和FAB滚动时:的https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling(part3)/

我已经贴什么样的行为看起来像下面的演示。

而不是在选项卡中recyclerview内的各个项目只持有一个TextView

现在,我有codeD它,使他们持有的画面(ImageView的),并在它下面,一个recyclerview呈现的项目清单。

因此​​,存在的外recyclerview携带内层recyclerviews的列表。

内recyclerview不滚动 - 我禁用它通过覆盖方法canScrollVertically()按照这个线程的答案:Disable滚动儿童Recyclerview机器人。我也试着使滚动的内部recyclerview,但我经历了同样的问题。

外recyclerview不滚动,并具有显示/隐藏工具栏与FAB的行为。

Android 入门第四讲03 列表RecyclerView RecyclerView使用步骤 详 ,RecyclerView指定一行item的数目 指定一行item的数量,并且设置列表方向

当我通过滚动持有到图片(ImageView的),应用程序行为工作完全正常,显示和隐藏工具栏和FAB。但是,当我有我的手指在内部recyclerview滚动,外recyclerview卷轴和列表上下移动,但显示的行为/隐藏工具栏和FAB是永远不会被激活。

我有一种感觉,这是因为内recyclerview截获滚动和外recyclerview没有得到滚动事件激活行为。

有谁知道如何确保外recyclerview也得到滚动事件,使行为的工作?

解决方案

汉克·穆迪的评论实际上导致了我正确的答案 - !感谢汉克

这是我如何解决我的问题:

创建一个滚动查看recyclerview那里的家长将收到孩子的所有滚动事件通过执行此操作:

公共类ScrollThroughRecyclerView扩展RecyclerView {     公共ScrollThroughRecyclerView(上下文的背景下){         超(上下文);     }     公共ScrollThroughRecyclerView(上下文的背景下,ATTRS的AttributeSet){         超(背景下,ATTRS);     }     公共ScrollThroughRecyclerView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){         超(背景下,ATTRS,defStyle);     }     @覆盖     公共布尔dispatchTouchEvent(MotionEvent EV){         //真正的 - 块子滚动,并允许滚动父回收 返回true;     } }

使用这个自定义recyclerview在XML或你的java code和滚动事件将被正确地传递给你的父母recyclerview这将激活该应用程序scrollbehavior。

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:

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;
    }
}

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.