我正在尝试使用具有以下模式的CardView创建RecyclerView布局:
在上图中,每个方形/矩形都是CardView。
我尝试了各种方法,包括创建两个不同的CardView布局,其中一个是全宽度CardView,另一个包含两个具有不同ID的CardView。
但是,当视图试图绑定更多布局时,实际上存在,因为一个布局文件包含两个子布局(2个卡片视图)。
是否有可以处理此问题的库?我也尝试了SpannedGridLayout library,但是如果你滚动到第一页之外会有一个小故障,它会崩溃。
任何帮助将不胜感激!
谢谢
答案 0 :(得分:3)
您必须使用自定义布局管理器,因为ADK中没有可以满足您需要的布局管理器。看看这里有一个很好的例子:
http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html
答案 1 :(得分:3)
您可以为不同的视图创建这样的适配器类
public class SampleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public Context context;
private ArrayList<String> stringArrayList;
public SampleAdapter(Context context) {
this.context = context;
}
public SampleAdapter(Context context, ArrayList<String> stringlist) {
this.stringArrayList = stringlist;
this.context = context;
notifyDataSetChanged();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View root;
switch (viewType) {
case 1:
root = inflater.inflate(R.layout.first_view, parent, false);
return new SecondViewHolder(root, viewType);
case 2:
root = inflater.inflate(R.layout.second_view, parent, false);
return new FirstViewHolder(root, viewType, context);
default:
return null;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
}
@Override
public int getItemViewType(int position) {
if (position % 2 == 0) {
return 1;
} else {
return 2;
}
}
@Override
public int getItemCount() {
return stringArrayList.size();
}
public static class FirstViewHolder extends RecyclerView.ViewHolder {
public FirstViewHolder(View itemView, int viewType, Context context) {
super(itemView);
}
}
public static class SecondViewHolder extends RecyclerView.ViewHolder {
public SecondViewHolder(View itemView, int viewType) {
super(itemView);
}
}
}
和first_view.xml
将是单个项目xml,如下所示:
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="100dp">
<LinearLayout
android:layout_width="match_parent"
android:background="@color/buttonRecordingNormal1"
android:layout_height="100dp">
</LinearLayout>
</LinearLayout>
您可以根据需要在此xml中设置cardview
而second_view.xml
将包含两个视图:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/buttonFocused2"></LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/buttonRecordingNormal2"></LinearLayout>
</LinearLayout>
</LinearLayout>
然后在你的活动中最终,
recyclerView = (RecyclerView) view.findViewById(R.id.recylerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
ArrayList<String>list=new ArrayList<>();
for(int i = 0 ;i <15; i ++)
{
list.add("");
}
sampleAdapter = new SampleAdapter(getActivity(), list);
recyclerView.setAdapter(sampleAdapter);
你可以传递bean对象列表等任何内容。我正在传递简单的字符串列表以显示视图和大约15项。
答案 2 :(得分:1)
您可以使用RecyclerView with GridLayoutManager
执行此操作TourEnum[] values = TourEnum.values();
创建列表并设置适配器
prepareList();
adapter = new MyAdapter(context, list);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 8);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
int pos = values[position % 8].points;
return pos;
}
});
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(gridLayoutManager);
将使用枚举类
创建模式public enum TourEnum {
ONE(4),
TWO(4),
THREE(8),
FOUR(4),
FIVE(4),
SIX(8),
SEVEN(4),
EIGHT(4);
//points determines amount of points each card is worth in Gin Rummy
public final int points;
//standard Rank constructor
TourEnum(int points) {
this.points = points;
}
}