【问题标题】:RecyclerView is not taking wrap_content height, it becomes scrollableRecyclerView 没有采用 wrap_content 高度,它变成可滚动的
【发布时间】:2018-03-06 12:29:36
【问题描述】:

详情:

依赖:(最新)

implementation 'com.android.support:recyclerview-v7:27.0.2'

XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_gray">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="@dimen/marginTwenty"
    android:layout_marginStart="@dimen/marginTwenty"
    android:layout_marginTop="@dimen/marginTen"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/contactLoadingLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="@dimen/marginThirty">

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/waitText"
            android:textAlignment="center"
            android:textStyle="bold" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/contactsLayoutView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/mobileView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/curved_background_white"
            android:orientation="vertical"
            android:padding="@dimen/marginTen">

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

                <ImageView
                    android:layout_width="@dimen/marginTwenty"
                    android:layout_height="@dimen/marginTwenty"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="5dp"
                    android:contentDescription="@string/dummyContent"
                    android:src="@drawable/ic_mobile" />


                <TextView
                    android:id="@+id/txtMobileNumber"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/mobileNumber"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/textSize_18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/mobileRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/txtNoMobileData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/marginTen"
                android:text="@string/no_contact_found"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold"
                android:visibility="gone" />

            <TextView
                android:id="@+id/btnAddMobile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:text="@string/add"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/emailView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marginTen"
            android:background="@drawable/curved_background_white"
            android:orientation="vertical"
            android:padding="@dimen/marginTen">


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

                <ImageView
                    android:layout_width="@dimen/marginTwenty"
                    android:layout_height="18dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="5dp"
                    android:contentDescription="@string/dummyContent"
                    android:src="@drawable/ic_email" />


                <TextView
                    android:id="@+id/txtEmailNumber"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/email"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/textSize_18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/emailRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/txtNoEmailData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/marginTen"
                android:text="@string/no_contact_found"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold"
                android:visibility="gone" />

            <TextView
                android:id="@+id/btnAddEmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:text="@string/add"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/facebookView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marginTen"
            android:background="@drawable/curved_background_white"
            android:orientation="vertical"
            android:padding="@dimen/marginTen">

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

                <ImageView
                    android:layout_width="@dimen/marginTwenty"
                    android:layout_height="@dimen/marginTwenty"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="5dp"
                    android:contentDescription="@string/dummyContent"
                    android:src="@drawable/ic_facebook" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/facebookHint"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/textSize_18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/facebookRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/txtNoFacebookData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/marginTen"
                android:text="@string/no_contact_found"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold"
                android:visibility="gone" />


            <TextView
                android:id="@+id/btnAddFacebook"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:text="@string/add"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linkedInView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marginTen"
            android:background="@drawable/curved_background_white"
            android:orientation="vertical"
            android:padding="@dimen/marginTen">

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

                <ImageView
                    android:layout_width="@dimen/marginTwenty"
                    android:layout_height="@dimen/marginTwenty"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="5dp"
                    android:contentDescription="@string/dummyContent"
                    android:src="@drawable/ic_linkedi" />

                <TextView
                    android:id="@+id/txtLinkedIn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/linkedInHint"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/textSize_18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/linkedInRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/txtNoLinkedInData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/marginTen"
                android:text="@string/no_contact_found"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold"
                android:visibility="gone" />

            <TextView
                android:id="@+id/btnAddLinkedIn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:text="@string/add"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/twitterView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marginTen"
            android:background="@drawable/curved_background_white"
            android:orientation="vertical"
            android:padding="@dimen/marginTen">

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

                <ImageView
                    android:layout_width="@dimen/marginTwenty"
                    android:layout_height="@dimen/marginTwenty"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="5dp"
                    android:contentDescription="@string/dummyContent"
                    android:src="@drawable/ic_twitter" />

                <TextView
                    android:id="@+id/txtTwitter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/twitterHint"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/textSize_18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/twitterRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/txtNoTwitterData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/marginTen"
                android:text="@string/no_contact_found"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold"
                android:visibility="gone" />

            <TextView
                android:id="@+id/btnAddTwitter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:text="@string/add"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/youtubeView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marginTen"
            android:background="@drawable/curved_background_white"
            android:orientation="vertical"
            android:padding="@dimen/marginTen">

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

                <ImageView
                    android:layout_width="@dimen/marginTwenty"
                    android:layout_height="@dimen/marginTwenty"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="5dp"
                    android:contentDescription="@string/dummyContent"
                    android:src="@drawable/ic_youtube" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/youtubeHint"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/textSize_18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/youtubeRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />


            <TextView
                android:id="@+id/txtNoYoutubeData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/marginTen"
                android:text="@string/no_contact_found"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold"
                android:visibility="gone" />

            <TextView
                android:id="@+id/btnAddYoutube"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:text="@string/add"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/gitHubView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/marginTwenty"
            android:layout_marginTop="@dimen/marginTen"
            android:background="@drawable/curved_background_white"
            android:orientation="vertical"
            android:padding="@dimen/marginTen">

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

                <ImageView
                    android:layout_width="@dimen/marginTwenty"
                    android:layout_height="@dimen/marginTwenty"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="5dp"
                    android:contentDescription="@string/dummyContent"
                    android:src="@drawable/ic_github" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY"
                    android:text="@string/gitHubHint"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/textSize_18sp"
                    android:textStyle="bold" />
            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/githubRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp" />

            <TextView
                android:id="@+id/txtNoGitHubData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/marginTen"
                android:text="@string/no_contact_found"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold"
                android:visibility="gone" />


            <TextView
                android:id="@+id/btnAddGithub"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:text="@string/add"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold" />
        </LinearLayout>

    </LinearLayout>

    <include
        android:id="@+id/noInternetLayoutContact"
        layout="@layout/no_internet_layout"
        android:visibility="gone" />

    </LinearLayout>

</ScrollView>

JAVA 代码:

youtubeRecyclerView.setLayoutManager(new 
LinearLayoutManager(getActContext(), LinearLayoutManager.VERTICAL, false));
youtubeRecyclerView.setHasFixedSize(true);
youtubeRecyclerView.setItemAnimator(new DefaultItemAnimator());
youtubeRecyclerView.setAdapter(attributeAdapter);
youtubeRecyclerView.setVisibility(View.VISIBLE);

注意:

  • 我在单个活动中有 6 个回收站视图,在这 6 个回收站视图中,任何一个回收站视图都可以滚动,其余部分可以正常工作(wrap_content),具体取决于屏幕大小。
  • 我的父视图是 ScrollView。

我对互联网和 stackoverflow 做了足够的研究,找不到解决方案,所以在这里提出问题。请不要标记为重复。

【问题讨论】:

  • android:adjustViewBounds="true" android:scaleType="fitXY" 是 imageView 的属性
  • 请发布整个 xml 文件。很难理解你想得到什么;)
  • 我的 recyclerview 没有这些属性,我试过只是实验它没有成功。
  • @muminers,添加了整个 XML 文件代码。
  • 试试this解决方案

标签: android android-recyclerview


【解决方案1】:

尝试使用固定高度的父级包装 RecyclerView

您可以选择固定高度wrap_content选择LinearLayout parent

          <LinearLayout
            android:id="@+id/llRecyclerContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marginTen">

             <android.support.v7.widget.RecyclerView
                android:id="@+id/facebookRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
          </LinearLayout>

在您的 java 文件中:

facebookRecyclerView.setHasFixedSize(true);

【讨论】:

  • 在这种情况下,如果尺寸很小怎么办?像2个项目?在这种情况下,它将有空白区域,这会使 UI 看起来很糟糕
  • RecyclerView 将在您要包装的固定区域中滚动
  • 这是我的问题,兄弟。我给了 recyclerview wrapcontent 高度,但是当列表长度增加 3 时它变得可滚动,这是我不想要的。即使列表大小增加 3,我也想设置所有 6 个可滚动的 recyclerview 的高度。
  • 请检查我的问题。我已经给了 youtubeRecyclerView.setHasFixedSize(true);
【解决方案2】:

scrollView 中使用RecycylerView 不是一个好习惯,因为它不支持视图回收。

最好使用一个垂直父级RecyclerView,并根据视图类型将您的孩子RecyclerView 添加到父级。

【讨论】:

    【解决方案3】:

    我设法通过将我的 recyclerview 放入 nestedscrollview 并保持 'android:nestedScrollingEnabled' 禁用/错误来解决此问题。

    【讨论】:

    • 在嵌套滚动视图中放置回收器视图会禁用视图回收,因此会破坏使用回收器视图的目的。如果您有大量数据,这些数据都将在创建时绘制,并会导致主线程滞后。
    猜你喜欢
    • 2015-05-04
    • 2022-08-24
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多