【发布时间】:2015-09-10 02:23:03
【问题描述】:
【问题讨论】:
-
你的意思是RecyclerView的每一项都包含2个CardView?
-
你也可以使用网格视图
-
如果每行有有限数量的卡片,则使用两张卡片创建一个行布局..
标签: java android android-recyclerview android-cardview
【问题讨论】:
标签: java android android-recyclerview android-cardview
我认为实现所附图片中描述的目标的正确方法是使用GridLayoutManager,而不是使用RecyclerView.LayoutManager 或LinearLayoutManager。
我们附加在RecyclerView 上的LayoutManager 决定了列数。大概有 3 个子类。
LinearLayoutManagerGridLayoutmangerStaggeredGridLayoutmanger在初始化RecyclerView.LayoutManager的活动中,更改
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManger(this);
到
GridLayoutManager mLayoutManager = new GridLayoutManger(this, 2);
2 是网格的跨度计数。每个项目都将放置在一个跨度中,因此您的回收站视图中有 2 列。
【讨论】:
试试这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="300dp"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/a"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Huming Bird"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@drawable/b"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Huming Bird"/>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
【讨论】:
你的 xml 可以这样做:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
</android.support.v7.widget.CardView>
</LinearLayout>
【讨论】:
要根据所需的列数动态设置,您可以根据列数设置布局管理器。这是非常灵活的。具有相应布局的相同/相似代码可以在平板电脑或手机上运行。
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
....
}
【讨论】: