【发布时间】: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