Android:ScrollView中的RecyclerView [英] Android: RecyclerView in a ScrollView

查看:175
本文介绍了Android:ScrollView中的RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ScrollView中使用RecyclerView.我的问题是,当我滚动RecyclerView时,它不是平滑"的:只要松开手指,滚动就会立即停止.

I'm trying to have a RecyclerView inside a ScrollView. My problem is that when i scroll the RecyclerView this is not "smooth": as soon as you release your finger the scrolling stops immediately.

我在片段中的布局是:

  • 滚动视图
    • LinearLayout
      • TextView
      • RecyclerView
      • 按钮
      • Scrollview
        • LinearLayout
          • TextView
          • RecyclerView
          • Button

          我尝试禁用嵌套滚动:

          mRecyclerView.setNestedScrollingEnabled(false)
          

          并包装mRecyclerView的内容:

          and wrapping content of mRecyclerView:

          android:layout_height="wrap_content"
          

          获得了我想要的平滑滚动,但是通过这种方式,RecyclerView的高度比它应该的要小(并且它不再滚动了,所以我看不到某些项目).

          obtaining the smooth scrolling i wanted, but in this way the height of the RecyclerView is smaller than it should (and it doesn't scroll anymore, so i can't see some items).

          我当前正在使用支持库23.4.0(我也尝试过23.2.1,同样的问题)

          I'm currently using support library 23.4.0 (i tried also 23.2.1, same problems)

          有帮助吗?

          推荐答案

          这个问题毁了我的夜晚. :)

          This problem has wrecked my night. :)

          RecyclerView在第一个屏幕上(不滚动即可显示)时,先前的视图会影响其高度.当RecyclerView最初完全不可见(需要向下滚动整个屏幕)时,其高度是正确的.

          When RecyclerView is on the first screen (visible without scrolling), preceding views impact its height. When RecyclerView is initially fully invisible (requires scrolling the whole screen down), its height is correct.

          ScrollView中的LinearLayout测量其子级时,它将在模式UNSPECIFIED下传递剩余多少像素作为MeasureSpec. RecyclerView将此规范传递给LayoutManager,并且它遵循传递的高度(如果它不为null,甚至为UNSPECIFIED).

          When LinearLayout inside ScrollView measures its children, it passes how many pixels left as a MeasureSpec with mode UNSPECIFIED. RecyclerView passes this spec to LayoutManager, and it obeys passed height, if it is not null, even it is UNSPECIFIED.

          扩展RecyclerView并覆盖RecyclerView#onMeasure,并且当高度测量规格为UNSPECIFIED时,只需将零传递给super.onMeasure.

          Extend RecyclerView and override RecyclerView#onMeasure and, when height measure spec is UNSPECIFIED, just pass zero to super.onMeasure.

          Java:

          @Override
          protected void onMeasure(int widthSpec, int heightSpec) {
              super.onMeasure(
                  widthSpec,
                  MeasureSpec.getMode(heightSpec) == MeasureSpec.UNSPECIFIED ? 0 : heightSpec
              )
          }
          

          科特琳:

          override fun onMeasure(widthSpec: Int, heightSpec: Int) {
              super.onMeasure(
                  widthSpec,
                  if (MeasureSpec.getMode(heightSpec) == MeasureSpec.UNSPECIFIED) 0
                  else heightSpec
              )
          }
          

          这篇关于Android:ScrollView中的RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆