简体   繁体   English

垂直Scrollview内的水平Recyclerview

[英]Horizontal Recyclerview inside a Vertical Scrollview

I have a situation where I have a Recyclerview which slides horizontally inside a ScrollView. 我有一种情况,我有一个Recyclerview在ScrollView内水平滑动。 Now the situation is when I do a horizontal swipe on the Recyclerview, instead of scrolling the cardviews inside Recyclerview, it scrolls the screen up, which disturbs the user experience. 现在情况是我在Recyclerview上进行水平滑动,而不是在Recyclerview中滚动卡片视图,它会向上滚动屏幕,这会干扰用户体验。

Any solution or approach to avoid vertical scrolling when the person is doing a horizontal swipe on Recyclerview? 当人在Recyclerview上进行水平滑动时,避免垂直滚动的任何解决方案或方法?

This problem is occurred due to scroll_conflicts As said by Frank. 由于scroll_conflicts发生了这个问题正如Frank所说。 But I understand sometimes we need to go beyond Design guidelines. 但我知道有时我们需要超越设计指南。 By the way you can disallow ScrollView (I am taking it as parent of RecyclerView) to intercept any touch events. 顺便说一句,您可以禁止ScrollView(我将其作为RecyclerView的父级)拦截任何触摸事件。 Following is the code sample for this. 以下是此代码示例。

mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    int action = event.getAction();
                    switch (action) {
                        case MotionEvent.ACTION_DOWN:
                            // Disallow ScrollView to intercept touch events.
                            v.getParent().requestDisallowInterceptTouchEvent(true);
                            break;
                        case MotionEvent.ACTION_UP:
                            //Allow ScrollView to intercept touch events once again.
                            v.getParent().requestDisallowInterceptTouchEvent(false);
                            break;
                    }
                    // Handle RecyclerView touch events.
                    v.onTouchEvent(event);
                    return true;
                }
            });

Hope this will work on your problem.But first check you LayoutManager. 希望这可以解决您的问题。但首先检查您的LayoutManager。 I think there is no need of above code if you explicitly use LinearLayoutManager with HORIZONTAL layout. 我认为如果明确使用带有HORIZONTAL布局的LinearLayoutManager,则不需要上面的代码。

The solution is actually quite simple. 解决方案实际上非常简单。 put the horizontal RecycleView as an item in a RecycleView, instead of using a scrollview. 将水平RecycleView作为项目放在RecycleView中,而不是使用scrollview。 then when you scroll it behaves as you want it to. 然后当你滚动它时你的行为就像你想要的那样。 Thats what I did and it works perfectly 多数民众赞成我所做的,它完美无缺

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

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