【问题标题】:RecyclerView that does not scroll and shows all items不滚动并显示所有项目的 RecyclerView
【发布时间】:2016-07-04 22:15:12
【问题描述】:

我在 ScrollView 中有一个 RecyclerView(和其他一些视图)。目前 RecyclerView 的布局非常小(它显示了它包含的 5 个项目中的 2 个)并且它独立于 ScrollView 滚动,这显然不是很好的用户体验。我想让 RecyclerView 不滚动并扩展,使其所有项目都可见。

(我知道在这种情况下使用 RecyclerView 很愚蠢。我这样做只是因为在应用程序的其他地方我需要一个普通的 RecyclerView 滚动等,但内容相同,我不想要复制代码)。

【问题讨论】:

  • ScrollView 不是为处理嵌套滚动而构建的。你查过NestedScrollView
  • 您是否设法在“NonScrollRecyclerView”中一次显示所有项目?由于某种原因,我添加超过 3 个项目后,我的 recyclerview 似乎没有重新计算它的高度
  • 面临类似问题。你想出解决办法了吗? @杰斐逊塔瓦雷斯?我使用了 wrap_content 并禁用了嵌套滚动。
  • 您可以手动设置RecyclerView的高度。
  • 在这种情况下使用 RecyclerView 并不愚蠢,因为您在其他人中解释了自己的原因;-)

标签: android android-recyclerview


【解决方案1】:

很简单,只需将RecyclerView的高度设置为wrap_content即可。

您还可以从禁用回收器视图上的嵌套滚动中受益,如下所示:

RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler);
recycler.setNestedScrollingEnabled(false);

【讨论】:

  • 没错。顺便说一句,我确实将RecyclerView 的高度设置为wrap_content。我面临的问题与使用毕加索的异步性获取视图和进出可见区域的视图的尴尬组合有关。这是一个完全独立的问答主题。
  • 你忘了说你必须使用 NestedScrollView 而不是 ScrollView。我在下面写过。
  • 采用这种方法,您是否不会在回收视图时丢失回收站视图,而只是在一个大滚动视图中创建一个包含所有视图的大列表?如果您尝试在该回收站视图中加载大量项目,则可能会“滞后”。
  • @William 是的,这就是 RecyclerView 上 wrap_content 的意义所在。它将绑定所有项目。我个人认为这是一个错误 - RecyclerView 应该在项目离开屏幕时回收,而不仅仅是当项目超出其边界时。但是我提交的错误被忽略了。所以现在你应该只对小型数据集使用 wrap_content,或者使用一个按钮来加载更多的项目。
  • @Natario 是的,我遇到了这个问题,为了管理它,我在nestedscrollview 滚动时手动计算recyclerview 的高度。这样做不会有任何滞后,而且速度非常快。
【解决方案2】:

setNestedScrollingEnabled(false) 的解决方案还不够完善:需要对 NestedScrollView 的子级使用 NestedScrollView 而不是 ScrollViewfocusableInTouchMode="true"。

如果你坚持使用 ScrollView,你也应该将 minHeight 设置为 RecyclerView,并设置 overScrollMode="never" 。在这种情况下,它仍然不是一个好的解决方案,因为在某些情况下 minHeight 可能不够

您应该考虑的其他替代解决方案:

  1. 将 ScrollView&RecyclerView 替换为单个 RecyclerView,该 RecyclerView 具有与 ScrollView 中的视图类型相同的视图

  2. 改用 GridLayout 或其他布局。

【讨论】:

  • scrollView 内使用recyclerView 会影响性能吗?
  • 如果你实现它,它不应该。
  • @VadimKotov 文档与什么有关?
  • @androiddeveloper 对 NestedScrollView、RecyclerView 的这种行为
  • @VadimKotov 对不起,这是很久以前的事了。
【解决方案3】:

也许乍一看并不完全清楚如何处理所有这些答案。 我刚刚尝试过,有效的是:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/person_properties"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
...
        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:overScrollMode="never" />
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

无需以编程方式更改任何内容。

【讨论】:

    【解决方案4】:

    在你的 activity.xml 文件中

    <androidx.core.widget.NestedScrollView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ActivityName">
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/RecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:nestedScrollingEnabled="false">
    
         </androidx.recyclerview.widget.RecyclerView>
    
    </androidx.core.widget.NestedScrollView>
    

    在 RecyclerView 中使用 android:nestedSrollingEnabled="false" 并使用 NestedScrollView 作为父 Scroll View。

    【讨论】:

    • 这是唯一对我有用的答案 - 我有一个包装两个 RecyclerViews 的 ScrollView。
    【解决方案5】:

    如果您在 ScrollView 中使用 RecyclerView,则将 ScrollView 替换为 NestedScrollView 并启用嵌套滚动

    android:nestedScrollingEnabled="false"
    

    这解决了我的问题

    【讨论】:

      【解决方案6】:

      一种简单的方法是在您的自定义适配器中使用 onBindViewHolder 方法中的这一行:holder.setIsRecyclable(false);

      【讨论】:

        【解决方案7】:

        我意识到我使用了BottomNavigationView,它阻止了我的回收站视图显示最后一个项目。我通过添加paddingBottom 来修复它:

            <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recipient_list"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingBottom="70dp"
                        app:layout_constraintTop_toBottomOf="@id/view"
                        />
        

        【讨论】:

          【解决方案8】:

          也尝试玩:

          android:overScrollMode
          

          【讨论】:

          • 同意,android:overScrollMode="never" 是一个很好的补充,因为它隐藏了过度滚动的稀松布
          【解决方案9】:

          您应该替换您的 scrollViewandroidx.core.widget.NestedScrollView 与 matchparent,它简单且工作正常。

          【讨论】:

            【解决方案10】:

            以下是在回收器视图中禁用滚动并显示布局中所有项目的代码。它可能会起作用:

            public class NoScrollRecycler extends RecyclerView {
            
                public NoScrollRecycler(Context context){
                    super(context);
                }
            
                public NoScrollRecycler(Context context, AttributeSet attrs){
                    super(context, attrs);
                }
            
                public NoScrollRecycler(Context context, AttributeSet attrs, int style){
                    super(context, attrs, style);
                }
            
                @Override
                public boolean dispatchTouchEvent(MotionEvent ev){
            
                    //Ignore scroll events.
                    if(ev.getAction() == MotionEvent.ACTION_MOVE)
                        return true;
            
                    //Dispatch event for non-scroll actions, namely clicks!
                    return super.dispatchTouchEvent(ev);
                }
            }
            

            这样使用:

            <com.example.custom.NoScrollRecycler
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/color_white"/>
            

            【讨论】:

              【解决方案11】:

              recyclerView 的父级可能是一个constraintLayout 将其更改为RelativeLayout

              【讨论】:

                猜你喜欢
                • 2020-09-23
                • 1970-01-01
                • 2021-10-08
                • 2021-01-19
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-11-06
                相关资源
                最近更新 更多