RecyclerView不滚动并显示所有项目

时间:2016-07-04 22:15:13

标签: android android-recyclerview

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

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

11 个答案:

答案 0 :(得分:44)

这很简单,只需将RecyclerView的高度设置为wrap_content

您可能还会受益于在回收器视图上禁用嵌套滚动,如下所示:

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

答案 1 :(得分:22)

setNestedScrollingEnabled(false)的解决方案并不完整:你需要使用NestedScrollView而不是ScrollViewfocusableInTouchMode =" true"到NestedScrollView的孩子。

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

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

  1. 使用单个RecyclerView替换ScrollView& RecyclerView,其中包含ScrollView中具有其他视图类型的视图

  2. 请改用GridLayout或其他布局。

答案 2 :(得分:8)

也许乍一看还不清楚如何处理所有这些答案。 我刚试过,工作的是:

<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>

无需以编程方式进行任何更改。

答案 3 :(得分:6)

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

android:nestedScrollingEnabled="false"

这解决了我的问题

答案 4 :(得分:2)

在您的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。

答案 5 :(得分:1)

也尝试玩:

var sorted = logInfo.Values.OrderBy(x=>x.Size);

答案 6 :(得分:1)

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

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"/>

答案 7 :(得分:1)

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

答案 8 :(得分:1)

您应该将您的 scrollView 替换为 androidx.core.widget.NestedScrollView ,并使其具有matchparent,并且操作简单。

答案 9 :(得分:0)

我意识到我使用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"
                />

答案 10 :(得分:0)

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

相关问题