【问题标题】:Make RecyclerView Scroll inside Fixed height使 RecyclerView 在固定高度内滚动
【发布时间】:2018-05-24 08:03:18
【问题描述】:

我想在Fixed 高度/区域内滚动ReyclerView

用于演示用例的示例 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ele_parent_ll"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent">


    <android.support.v4.widget.NestedScrollView
        android:id="@+id/election_parent_sv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:id="@+id/sample_id"
            android:background="@color/white"
            android:fitsSystemWindows="true">

            <ImageView
                android:contentDescription="@string/btn_youtube"
                android:layout_width="match_parent"
                android:layout_height="115dp"
                android:id="@+id/election_banner"
                android:background="@drawable/some_drawable"
                android:adjustViewBounds="true"/>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">



                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/mid_parent"
                    android:layout_marginLeft="@dimen/view_margin_10"
                    android:layout_marginRight="@dimen/view_margin_10"
                    android:layout_marginTop="@dimen/view_margin_10"
                    android:orientation="horizontal"
                    android:weightSum="1">

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight=".5"
                        android:orientation="vertical">

                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@drawable/shaded_purple_new_local"
                            android:text="Layout heading"
                            android:textColor="@color/white" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:text="Choose one"
                            android:textColor="@color/icon_yellow"
                            android:textSize="13sp" />


                        <android.support.v7.widget.RecyclerView
                            android:id="@+id/candidate_recycler"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:nestedScrollingEnabled="false">
                        </android.support.v7.widget.RecyclerView>
                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="@dimen/view_margin_10"
                        android:layout_marginLeft="@dimen/view_margin_10"
                        android:layout_weight=".5"
                        android:orientation="vertical">
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>

            <!--Sample Layout to make the screen scrollable for the example-->
            <RelativeLayout
                android:id="@+id/samp_RL"
                android:layout_width="match_parent"
                android:layout_height="200dp">
            </RelativeLayout>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</LinearLayout>

在这种情况下,RecyclerView 占用了它所需的空间,并且列表非常长,因此可滚动区域的总长度变长了,因此看到低于 RCV 的元素的可能性降低了。

为了解决这个问题,我想在固定区域内滚动RecyclerView,这样如果用户对此不感兴趣,他可以轻松查看RecyclerView下方的其他元素

  1. 当用户在 RCV 区域内滚动时,RCV 应该滚动。
  2. 当用户在 RCV 外滚动时,整个布局应该滚动

图片来自draw.io

我尝试过的:

  1. 使 RecyclerView 的高度保持不变:它被卡住了,根本不滚动。
  2. Making Recyclerview Fixed Height and Scrollable:也一样。它卡住了,根本不滚动。
  3. Scrollable NestedScrollViews inside RecyclerView:试过了,但无法与我目前的情况建立联系。

请告诉我这里缺少什么。

【问题讨论】:

    标签: java android xml android-recyclerview


    【解决方案1】:

    你不需要做任何事情。默认情况下它只像这样工作! 试试下面的代码 -

    recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
            recyclerView.setHasFixedSize(false);
            layoutManager = new LinearLayoutManager(getActivity());
            recyclerView.setLayoutManager(layoutManager);
            mAdapter = new MyAdapterClass(getContext(), lst);
            recyclerView.setAdapter(mAdapter);
    

    XML

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
        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="com.example.iosdteachingapp.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="100sp"
            android:text="hello"/>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/my_recycler_view"
            android:layout_width="200sp"
            android:layout_height="300sp" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="end"
            android:padding="20sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="end"
            android:padding="20sp"/> <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="end"
        android:padding="20sp"/> <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="end"
        android:padding="20sp"/>
    </LinearLayout>
    </ScrollView>
    

    【讨论】:

    • 您好 deepanshi,我尝试了您的代码,但它不能使 recyclerView 独立滚动。它只显示其高度内的内容并且不滚动,即它被卡住了。我在上面的问题中给出了我的 XML,请使用它,看看你是否可以帮助我。谢谢。
    • 我已经尝试过了,并制作了与您展示的相同的用户界面及其工作
    【解决方案2】:

    android:nestedScrollingEnabled="false" 添加到您的 RecyclerView。

    【讨论】:

    • 我是否必须将它包装在任何其他自定义布局或您缺少的东西中?我目前给它一个固定大小为android:layout_height="200dp" 因为它现在不工作
    • 你试过我的解决方案了吗?
    • 是的。我之前已经在使用它来使 RCV 中的滚动平滑
    【解决方案3】:

    使用参数通过 java 动态设置 Recycler 视图高度。 您甚至可以通过设置高度 = 回收站视图的每个元素的大小 * 列表中的元素数量来设置高度以仅显示特定数量的元素。 试试这个:

    ViewGroup.LayoutParams 
    params=recyclerview.getLayoutParams();
    params.height=100;
    recyclerview.setLayoutParams(params);
    

    【讨论】:

    • 您好 Sparsh,我尝试了您的代码,但它不会使 recyclerView 独立滚动。它只显示其高度内的内容并且不滚动,即它被卡住了。我在上面的问题中给出了我的 XML,请使用它,看看你是否可以帮助我。谢谢。
    猜你喜欢
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 2011-01-23
    • 2012-06-14
    相关资源
    最近更新 更多