【问题标题】:Making more thatn one card view to be visible in horizontal recyclerview使多个卡片视图在水平回收视图中可见
【发布时间】:2016-08-05 22:18:31
【问题描述】:

我正在尝试创建包含多个 carview 项目的水平滑动回收器视图。以下是我如何设置适配器的方式

 LinearLayoutManager horizontalLayoutManagaer
                    = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
            horizontal_recycler_view.setLayoutManager(horizontalLayoutManagaer);
            horizontal_recycler_view.setItemAnimator(new DefaultItemAnimator());
            horizontal_recycler_view.setAdapter(adapter);

以下是我的recyclerview项目代码布局代码。

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="8dp"
>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/album_cover_height"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:clickable="true"
            android:scaleType="fitXY" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/thumbnail"
            android:paddingLeft="@dimen/album_title_padding"
            android:paddingRight="@dimen/album_title_padding"
            android:paddingTop="@dimen/album_title_padding"
            android:textColor="@color/album_title"
            android:textSize="@dimen/album_title" />

        <TextView
            android:id="@+id/count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:paddingBottom="@dimen/songs_count_padding_bottom"
            android:paddingLeft="@dimen/album_title_padding"
            android:paddingRight="@dimen/album_title_padding"
            android:textSize="@dimen/songs_count" />

        <ImageView
            android:id="@+id/overflow"
            android:layout_width="@dimen/ic_album_overflow_width"
            android:layout_height="@dimen/ic_album_overflow_height"
            android:layout_alignParentRight="true"
            android:layout_below="@id/thumbnail"
            android:layout_marginTop="@dimen/ic_album_overflow_margin_top"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_dots"
            />

    </RelativeLayout>

</android.support.v7.widget.CardView>

问题是一次只能看到一张卡片,但我希望像在 google play 中一样看到不止一张卡片。我怎样才能解决这个问题?

如您所见,布局包含两个水平回收器视图。然后它包含垂直回收器视图。水平回收器视图仅显示一个卡片视图,并在滑动时显示其他视图,但我想同时显示多个视图。

【问题讨论】:

  • 不需要使用父线性布局。 Parent 应该是您的 CardView,然后将 CardView 的宽度/高度设置为 wrap_content 或任何修复 dp。
  • 如果我删除线性布局,它会显示布局错误
  • 你必须设置你的cardview的固定宽度
  • 如何设置cardview的固定宽度?

标签: android android-recyclerview


【解决方案1】:

试试

  1. 将您的父 LinearLayout 以及子视图宽度设置为 wrap_content。
  2. ID 为overflow 的ImageView 与父级的右侧对齐。删除它并添加android:layout_alignRight="@id/thumbnail"
  3. 还可以尝试清理并重新构建您的项目,因为有时布局代码更改不会在构建时反映出来。

【讨论】:

  • 我删除了 alignParentRight,我按照你说的做了,但问题仍然存在
  • @Learner 在你Android Studio的预览页面,这个布局是怎么预览的??那里也完全伸展了吗??
  • 在布局预览中,没有完全拉伸,只在设备中拉伸
  • @sJy 你应该编辑你的答案,或者至少在这里评论你通过清理和重建修复的问题,我也在聊天中提出了一些建议:/
【解决方案2】:

您应该为所有 ViewGroups(即 LinearLayout、CardView 和 RelativeLayout)将 layout_width 设置为 wrap_content,仅设置外部 ViewGroups 是不够的。

这也适用于所有其他子视图,例如您的 ImageView、TextView 等。通常,如果子视图将 layout_width 设置为 match_parent 并且父视图具有 wrap_content,则父视图将尝试变大尽可能地适应这一点。

还有一点风格,你应该用骆驼符号命名你的变量,例如horizontal_recycler_view 应该是 horizontalRecyclerView。

【讨论】:

  • 我已经尝试过了,但同样的错误仍然存​​在。我也发布了屏幕截图。请阅读更新的问题而不是您
  • 内部RelativeLayout中包含的所有视图也应该将layout_width设置为wrap_parent,我已经更新了我的答案以显示这一点
  • 我已将所有内容更改为 wrap_content 但问题仍然存在。我什至硬编码了 dp 中的宽度值,但仍然是同样的问题
  • 可能你的图片很大,尝试在你的 ImageView 中将scaleType 设置为fitCenter
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
  • 2016-07-22
  • 1970-01-01
  • 2021-06-08
相关资源
最近更新 更多