java NestedScrollView 没有在里面放 Recyclerview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37384640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:26:58  来源:igfitidea点击:

NestedScrollView not fling with Recyclerview inside

javaandroid

提问by Khang .NT

I have a layout like that:

我有这样的布局:

<NestedScrollView>
     <RecyclerView> // vertical recycler view
          <RecyclerView/>  // horizontal recycler view
          <RecyclerView/>
          <RecyclerView/>
          ...
     <RecyclerView>
</NestedScrollView>

The result looks like Google play store: enter image description here

结果看起来像 Google Play 商店: 在此处输入图片说明

And I disabled NestedScrolling in horizontal Recyclerview:

我在horizontal Recycler视图中禁用了 NestedScrolling :

horizontalRecyclerView.setHasFixedSize(true);
horizontalRecyclerView.setNestedScrollingEnabled(false);

My problem:

我的问题:

The vertical recyclerviewdoes not scroll fling, whenever ACTION_UPhappen, the vertical recyclerviewalso stop scrolling.

vertical recyclerview不滚动一扔,每当ACTION_UP发生了,vertical recyclerview也停止滚动。

How can I nest vertical recyclerviewinside nestedscrollview, and horizontal recyclerviewinside vertical recyclerviewlike Playstore and keep the scroll smooth.

我怎么能窝vertical recyclerviewnestedscrollview,和horizontal recyclerview里面vertical recyclerview像Play商店中,并保持滚动顺畅。

Solved:

解决了:

Using custom nested scroll view of @vrund purohit (code below), and disabled nestedscroll both vertical and horizontal recyclerview:

使用@vrund purohit 的自定义嵌套滚动视图(下面的代码),并禁用了垂直和水平回收器视图的nestedscroll:

verticalRecyclerView.setNestedScrollingEnabled(false);
... add each horizontal recyclerviews:
horizontalRecyclerView.setNestedScrollingEnabled(false);

采纳答案by V-rund Puro-hit

I had this same problem and I solved this issue by customizing NeatedScrollView.

我遇到了同样的问题,我通过自定义NeatedScrollView.

Here is the class for that.

这是课程。

MyNestedScrollView

我的嵌套滚动视图

public class MyNestedScrollView extends NestedScrollView {
    @SuppressWarnings("unused")
    private int slop;
    @SuppressWarnings("unused")
    private float mInitialMotionX;
    @SuppressWarnings("unused")
    private float mInitialMotionY;
    public MyNestedScrollView(Context context) {
        super(context);
        init(context);
    }
    private void init(Context context) {
        ViewConfiguration config = ViewConfiguration.get(context);
        slop = config.getScaledEdgeSlop();
    }
    public MyNestedScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
    public MyNestedScrollView(Context context, AttributeSet attrs,
            int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    private float xDistance, yDistance, lastX, lastY;
    @SuppressWarnings("unused")
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final float x = ev.getX();
        final float y = ev.getY();
        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            xDistance = yDistance = 0f;
            lastX = ev.getX();
            lastY = ev.getY();
            // This is very important line that fixes
            computeScroll();
            break;
        case MotionEvent.ACTION_MOVE:
            final float curX = ev.getX();
            final float curY = ev.getY();
            xDistance += Math.abs(curX - lastX);
            yDistance += Math.abs(curY - lastY);
            lastX = curX;
            lastY = curY;
            if (xDistance > yDistance) {
                return false;
            }
        }
        return super.onInterceptTouchEvent(ev);
    }
    public interface OnScrollChangedListener {
        void onScrollChanged(NestedScrollView who, int l, int t, int oldl,
                int oldt);
    }
    private OnScrollChangedListener mOnScrollChangedListener;
    public void setOnScrollChangedListener(OnScrollChangedListener listener) {
        mOnScrollChangedListener = listener;
    }
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mOnScrollChangedListener != null) {
            mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt);
        }
    }
}

Happy coding.

快乐编码。

回答by Kuldeep Sakhiya

Use below code for smooth scroll:

使用以下代码进行平滑滚动:

ViewCompat.setNestedScrollingEnabled(recyclerView, false);

回答by jbc25

Add this in your RecyclerView xml:

在您的 RecyclerView xml 中添加:

android:nestedScrollingEnabled="false"

回答by Amit

[RESOLVED] I have same issue with Horizontal recycleview. Change Gradle repo for recycleview

[已解决] 我对 Horizo​​ntal recycleview 有同样的问题。更改 Gradle repo 以进行 recycleview

compile 'com.android.support:recyclerview-v7:23.2.1' Write this: linearLayoutManager.setAutoMeasureEnabled(true);

编译 'com.android.support:recyclerview-v7:23.2.1' 写这个:linearLayoutManager.setAutoMeasureEnabled(true);

Fixed bugs related to various measure-spec methods in update

修复了更新中与各种测量规范方法相关的错误

Check http://developer.android.com/intl/es/tools/support-library/features.html#v7-recyclerview

检查http://developer.android.com/intl/es/tools/support-library/features.html#v7-recyclerview

I have found issue with 23.2.1 library: When item is match_parent recycle view fill full item to view, please always go with min height or "wrap_content".

我发现了 23.2.1 库的问题:当项目是 match_parent 回收视图填充完整项目以查看时,请始终使用最小高度或“wrap_content”。

Thanks

谢谢

回答by Waqar UlHaq

I've solved the issue by using below code:

我已经使用下面的代码解决了这个问题:

        myRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false){
        @Override
        public boolean canScrollHorizontally() {
            return true;
        }

        @Override
        public boolean canScrollVertically() {
            return true;
        }
    });