在水平回收视图

时间:2016-08-05 11:49:00

标签: android android-recyclerview

我正在尝试创建包含多个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中可以看到多张卡片。我怎样才能解决这个问题?

A

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

2 个答案:

答案 0 :(得分:3)

尝试

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

答案 1 :(得分:1)

您应该为所有ViewGroup(即LinearLayout,CardView和RelativeLayout)设置layout_widthwrap_content,仅仅设置外部ViewGroup是不够的。

这也适用于所有其他子项,例如ImageView,TextView等。通常,如果子视图的layout_width设置为match_parent且父级有wrap_content,则父级我会努力尽可能地满足这一需求。

同样是一种风格,你应该用骆驼符号命名你的变量,例如: horizontal_recycler_view应为horizontalRecyclerView

相关问题