2017-05-03 58 views
0

我想创建一个RecyclerView,其中有两个CardView可以在其内部可见,并且可以水平滚动。类似于下面的图片。 enter image description here如何用水平滚动来完成这个RecyclerView?

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

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

enter image description here

设置布局管理该代码创建一个不正确的格式,但它横向滚动。

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

enter image description here

我的问题是我怎么创建两个CardViews是并排的,并且可以水平滚动的格式?

这里是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> 
+0

您是否尝试过使用LinearLayoutManager而不是GridLayoutManager? –

回答

1

尝试使用LinearLayoutManager与水平方向:

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

如果你想在同一时间显示2张卡,将其宽度设置为屏幕大小的一半。

+0

谢谢你的回答。我曾考虑过使用LinearLayoutManager,但问题在于如何均衡内部的两个CardView的宽度,以便它们在父级中相同。另外如果我想现在显示三个CardView,会发生什么?这不会像使用GridLayoutManager和更改列数一样简单。 – John

+0

你可以做'cardWidth = screenSize/COLUMNS_NUMBER',如果你想添加更多的列,只需改变最后一个常量。 'GridLayoutManager'工作,如果你想使一个多行和一列的网格,而不是你的情况。 –

+0

或者你可以尝试在你的CardView中使用'android:weight',但是我不知道它是否有效 –