我有类似的东西,我解决与新的RecyclerView。
我创建了一个片段与一个RecyclerView。
xml上的RecyclerView:
android:id="@+id/filter_subtypes" android:layout_width="match_parent" android:layout_height="match_parent" />
在你的Fragment / Activity(OnViewCreated在我的片段case)。
我找到RecyclerView并设置一个适配器 – 一个继承自RecyclerView的正常适配器类。您的查看持有人类> –
然后我创建一个GridLayoutManager
final GridLayoutManager mng_layout = new GridLayoutManager(this.getActivity(), TOTAL_CELLS_PER_ROW/*In your case 4*/);
然后我覆盖这个方法来设置动态列数(单元格)
mng_layout.setSpanSizeLookup( new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
switch( adapterSubtype.getItemViewType(position) ) {
case FilterSubtypesAdapter.TYPE_LOW:
return TOTAL_CELLS_PER_ROW;
case FilterSubtypesAdapter.TYPE_HIGH:
return 2;
default:
return -1;
}
}
});
myRecyclerView.setLayoutManager(mng_layout);
有了这个,你会得到动态行数上的单元格。
额外:
然后,如果您在适配器上使用相同的视图/类型视图,您将获得相同的w& h视图。您需要为TYPE_HIGH创建2个xml视图,为TYPE_LOW创建其他视图。
所以,在你的适配器,你需要有2种数据(1为高图像和1为低图像)。
您必须覆盖此方法
@Override
public SubtypeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
if (viewType==TYPE_HIGH) {
view = inflater.inflate(R.layout.item_image_high, parent, false);
} else {
view = inflater.inflate(R.layout.item_image_low, parent, false);
}
return new SubtypeViewHolder(view, viewType);
}
@Override
public int getItemViewType(int position) {
return (list.get(position).getType()==Subtype_type.HIGH) ? TYPE_HIGH : TYPE_LOW;
}
我希望我很清楚,任何问题告诉我。