如何使用水平滚动完成此RecyclerView?

时间:2017-05-03 21:11:57

标签: android android-recyclerview

我想创建一个RecyclerView,其中可以看到两个可以水平滚动的CardView。类似于下图。 enter image description here

将LayoutManager设置为此代码会创建所需的格式,但它会垂直滚动。

recyclerView.SetLayoutManager(new GridLayoutManager(Context, 2, GridLayoutManager.Vertical, false));

enter image description here

将LayoutManager设置为此代码会创建不正确的格式,但它会水平滚动。

recyclerView.SetLayoutManager(new GridLayoutManager(Context, 2, GridLayoutManager.Horizontal, false));

enter image description here

我的问题是如何创建两个并排的CardView格式并可以水平滚动?

以下是CardView XML以防万一:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/cardView"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp">
  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#FF0000">
  </RelativeLayout>
</android.support.v7.widget.CardView>

1 个答案:

答案 0 :(得分:1)

尝试使用水平方向的LinearLayoutManager:

LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);

如果您想同时显示2张卡片,请将其宽度设置为屏幕尺寸的一半。

相关问题