NestedScrollView里面没有使用Recyclerview

时间:2016-05-23 07:09:18

标签: java android

我有这样的布局:

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

结果看起来像Google Play商店: enter image description here

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

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

我的问题:

vertical recyclerview不会滚动,每当ACTION_UP发生时,vertical recyclerview也会停止滚动。

如何将vertical recyclerview置于nestedscrollview内,horizontal recyclerview置于Playstore内的vertical recyclerview内,并保持滚动顺畅。

解决:

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

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

5 个答案:

答案 0 :(得分:52)

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

ViewCompat.setNestedScrollingEnabled(recyclerView, false);

答案 1 :(得分:8)

我遇到了同样的问题,我通过自定义NeatedScrollView解决了这个问题。

这是上课。

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

快乐的编码。

答案 2 :(得分:5)

在RecyclerView xml中添加:

android:nestedScrollingEnabled="false"

答案 3 :(得分:4)

[已解决]我对Horizo​​ntal recycleview也有同样的问题。更改gradle repo for recycleview

compile&#39; com.android.support:recyclerview-v7:23.2.1&#39; 写下:linearLayoutManager.setAutoMeasureEnabled(true);

修复了与更新中的各种measure-spec方法相关的错误

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

我发现了23.2.1库的问题:当item为match_parent时,回收视图填充要查看的完整项目,请始终使用最小高度或&#34; wrap_content&#34;。

由于

答案 4 :(得分:1)

我使用以下代码解决了这个问题:

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

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