【问题标题】:NestedScrollView not fling with Recyclerview insideNestedScrollView 里面没有 Recyclerview
【发布时间】:2016-05-23 07:09:18
【问题描述】:

我有这样的布局:

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

结果看起来像 Google Play 商店:

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

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

我的问题:

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

如何像 Playstore 一样将 vertical recyclerview 嵌套在 nestedscrollview 中,并将 horizontal recyclerview 嵌套在 vertical recyclerview 中并保持滚动流畅。

已解决:

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

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

【问题讨论】:

标签: java android


【解决方案1】:

在你的 RecyclerView xml 中添加这个:

android:nestedScrollingEnabled="false"

【讨论】:

  • 这仅适用于 API 级别 21 及以上,低于 API 级别 21 请参阅 Kuldeeps 答案。
  • 如果你在布局中使用数据绑定,你也可以创建一个 bindingadapter:@BindingAdapter("nestedScrollingEnabled") public static void setNestedScrollingEnabled(final View view, final boolean value) { ViewCompat.setNestedScrollingEnabled(view , 价值); } 然后使用 app:nestedScrollingEnabled="@{false}" 添加到布局
【解决方案2】:

我遇到了同样的问题,我通过自定义 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);
        }
    }
}

编码愉快。

【讨论】:

【解决方案3】:

使用以下代码平滑滚动:

ViewCompat.setNestedScrollingEnabled(recyclerView, false);

【讨论】:

  • 谢谢!完美运行。
  • 把它放在你的MainActivity.onCreate或者你创建recyclerView的地方
  • 完美答案!也在 xml 中工作:android:nestedScrollingEnabled="false" 谢谢!
  • 毫无疑问的最佳答案!
【解决方案4】:

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

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

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

【讨论】:

    【解决方案5】:

    [已解决] 我对 Horizo​​ntal recycleview 也有同样的问题。为 recycleview 更改 Gradle 存储库

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

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

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

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

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-10
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      相关资源
      最近更新 更多